开发者

@protocol on IOS parsing data 2 way?

开发者 https://www.devze.com 2023-03-07 09:10 出处:网络
I have login Code like this : @protocol LoginDelegate -(void)DUsername:(NSString *) username DPassword:(NSString *) password;

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 :

@interface mainViewController : UIViewController<LoginDelegate> {

and call this methode on 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).

My Question how i can parsing data from mainViewController to This login 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

A.h:

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

B.h:

#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)

A.h:

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

B.h:

#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

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 is often used in a ModelViewController (MVC) Pattern. 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)

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

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

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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消