简体   繁体   English

@protocol关于IOS解析数据的2种方式?

[英]@protocol on IOS parsing data 2 way?


I have login Code like this : 我有这样的登录代码:

@protocol LoginDelegate 

-(void)DUsername:(NSString *) username DPassword:(NSString *) password;

@end


@interface loginipad : UIViewController {
    id<LoginDelegate> delegate;
    IBOutlet UITextField *edusername;
    IBOutlet UITextField *edpassword;
}

and then i use this object on mainViewController like this : 然后我在mainViewController上使用此对象,如下所示:

@interface mainViewController : UIViewController<LoginDelegate> {

and call this methode on mainViewController 并在mainViewController上调用此方法

-(void)DUsername:(NSString *) username DPassword:(NSString *) password{
    userlogin=[username retain];
    passlogin=[password retain];
    if (!scm.isRunning) {
        [scm connectToHost:@"localhost" onPort:8080];
    }    
}

This method is success to parsing data from login modalview to mainViewController, but i want show progress of process or any message from mainViewController to login modal view when login button is klick (i try MBPrgoressHUD but no success due i use this login on modal view). 此方法成功解析了从登录modalview到mainViewController的数据,但是当单击登录按钮时,我想显示进程进度或从mainViewController到登录模式view的任何消息(我尝试使用MBPrgoressHUD,但由于在模式视图上使用此登录而没有成功) 。

My Question how i can parsing data from mainViewController to This login modalview ? 我的问题如何将数据从mainViewController解析到此登录modalview?
Thanks, 谢谢,

for call the method : 调用方法:

loginipad *plogin = [[loginipad alloc] initWithNibName:@"loginipad"  bundle:nil];
    plogin.delegate = self;
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:plogin];
    plogin.title=@"Login";


    [self presentModalViewController:nc animated:YES];


    [nc release];
    nc = nil;
    [plogin release];
    plogin = nil;

answer completely edited 答案完全编辑

Your question leads to multiple solutions and strategies. 您的问题导致了多种解决方案和策略。

First: general posibilities to implement bidirectional data-transfer between two classes. 第一:实现两类之间的双向数据传输的一般可能性。

via multiple protocols: loose cupling but leads to import-loops which are annoying. 通过多种协议:松散的装杯,但会导致令人讨厌的导入循环。 I know ho to solve import loops for class-definitions (@class) but I dont know how to solve this for protocols 我知道如何解决类定义(@class)的导入循环,但我不知道如何为协议解决此问题

Ah: 啊:

#import "B.h"
@protocol ADelegate
-(void) adelegate:(NSString*)data;
@end
@interface A : NSObject<BDelegate>
{
    id<ADelegate> delegate;
}
@end

Bh: Bh:

#import "A.h"
@protocol BDelegate
-(void) bdelegate:(NSString*)data;
@end
@interface B : NSObject<ADelegate>
{
    id<BDelegate> delegate;
}
@end

via a single protocol: dense cupling :( but no import-loop (this is a working ugly style) 通过一个协议:密集的cupling :(但是没有导入循环(这是一个有效的丑陋风格)

Ah: 啊:

//no import here needed
@protocol ADelegate
-(void) adelegate:(NSString*)data;
@end
@interface A : NSObject<BDelegate>
{
    id<ADelegate> delegate;
}
@end

Bh: Bh:

#import "A.h"
@interface B : NSObject<ADelegate>
{
    A* delegate;
}
@end

via pipe/stream: bidirectional data-transfer should by done using a pipe (unbuffered) or stream (buffered) here I show you a small and simple delegate-pipe but there also exists a NSPipe/NSStream 通过管道/流:双向数据传输应使用管道(无缓冲)或流(缓冲)完成,在这里我向您展示了一个小而简单的委托管道,但也存在一个NSPipe / NSStream

DelegatePipe.h DelegatePipe.h

@protocol DelegatePipeDelegate
- dataArrived:(NSString*)data;
@end

@interface DelegatePipe : NSObject {
    NSMutableArray *delegates;
}
-(void)open:(id<DelegatePipeDelegate>)d;
-(void)close:(id<DelegatePipeDelegate>)d;
-(void)send:(NSString*)data;

@end

DelegatePipe.m 代表管道

@implementation DelegatePipe

-(id)init
{
    if(self = [super init])
    {
        delegates = [NSMutableArray array];
    }
    return self;
}
-(void) dealloc
{
    [delegates release];
    delegates = nil;
}
-(void) open:(id <DelegatePipeDelegate>)d
{
    @synchronized(self)
    {
        if([delegates containsObject:d])
            return;
        //if([delegates count]>=2) //Pipe contains originally only 2 delegates. but a broadcaster is also nice ;)
        //  return; 
        [delegates addObject:d];
    }
}
-(void) close:(id <DelegatePipeDelegate>)d
{
    @synchronized(self)
    {
        [delegates removeObject:d];
    }
}
-(void) send:(NSString *)data
{
    @synchronized(self)
    {
        for(id<DelegatePipeDelegate> d in delegates)
            [d dataArrived:data];
    }
}
@end

Second: KVO 第二名:KVO

KVO is often used in a ModelViewController (MVC) Pattern. KVO通常用于ModelViewController(MVC)模式中。 eg: visualize data in a view. 例如:在视图中可视化数据。 The network-connection-state in your case is part of data and your loginipad is a view ( and a controller ) 在您的情况下,网络连接状态是数据的一部分,而loginipad是视图( 和控制器

Authentificator.h 验证者

typedef enum eAuthState
{
    NOT_CONNECTED = 0,
    LOGIN_FAILED,
    CONNECING,
    CONNECTED
} AuthState;

@interface Authentificator : NSObject {
    AuthState state;
}
@property (nonatomic, assign) AuthState state;
@end

Authentificator.m 验证者

...

-(void) doAuthWithUsername:(NSString*)name password:(NSString*)pw
{
    self.state = CONNECING;
    //do network-stuff
}

//delegate from network. here NSURLConnection
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    //parse network-answer
    BOOL success = YES;

    if(success)
        self.state = CONNECTED;
    else
        self.state = LOGIN_FAILED;  
}

loginipad.h loginipad.h

@interface loginipad : UIViewController 
{
    Authentificator *auth;
}
@property (nonatomic, retain) Authentificator *auth;
@end

loginipad.m loginipad.m

@implementation loginipad
@synthesize auth;

//override setter for more comfortable use (add/removeObserver)
-(void) setAuth:(Authentificator *)a
{
    @synchronized(auth)
    {
        [auth removeObserver:self forKeyPath:@"state"];
        [auth release];
        auth = a;
        [auth retain];
        [auth addObserver:self forKeyPath:@"state" options:0 context:nil];  
    }
}

-(IBAction) buttonClicked:(id)aSender
{
    self.auth = [Authentificator sharedAuthentificator];
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if(![object isKindOfClass:Authentificator.class])
        return;
    AuthState state = ((Authentificator*)object).state;
    NSLog(@"curState: %i",state);
    //do sth with state
}

- (void)dealloc {
    self.auth = nil;
    [super dealloc];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM