开发者

Objective c: Protocol + Delegate to pass data back from Login form in a Modal View to an tab bar controller view

开发者 https://www.devze.com 2023-03-25 10:47 出处:网络
I\'m developing a tab bar based app for iPhone. The flow is the following: when the app runs, I throw up the modal view with the login form:

I'm developing a tab bar based app for iPhone. The flow is the following: when the app runs, I throw up the modal view with the login form:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpt开发者_开发百科ions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.
tabBarController.delegate = self;

// Add the tab bar controller's view to the window and display. 
self.window.rootViewController = self.tabBarController;
[self addTabBarArrow];

LoginViewController *loginViewController = [[LoginViewController alloc] init];;
[window addSubview:tabBarController.view];
[self.tabBarController presentModalViewController:loginViewController animated:YES];
[window makeKeyAndVisible];
return YES; }

In the log in modal view LoginViewController.h (child) I've got a protocol implemented:

@protocol PassUserInfoDelegate <NSObject>
@required
- (void) passUserInfo: (NSString *)string;
@end

When the user fills out the form, I create a NSURLConnection, and in the connectionDidFinishLoading method I get the user values from an JSON request:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *respuestaServidor = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;

NSDictionary *dictionary = [respuestaServidor JSONValue];
idJson = [dictionary objectForKey:@"id"];
NSString *user_loginJson = [dictionary objectForKey:@"user_login"];

if ([idJson isEqualToString:@"null"] && [user_loginJson isEqualToString:@"null"]) {
    NSLog(@"Login incorrecto");
} else {
    NSLog(@"Procedo a loguear usuario");
}
[indicator stopAnimating];
[indicator release];
}

In HomeViewController.h (parent) I've got the delegation:

@interface HomeViewController : UIViewController <PassUserInfoDelegate> {
LoginViewController *protocolTest;
IBOutlet UILabel *nombreUsuario;
NSString *usuario;
}
@property (nonatomic, retain) IBOutlet UILabel *nombreUsuario;
@property (copy) NSString *usuario;

- (void) passUserInfo:(NSString *)string;

@end

and in the HomeViewController.m I implement the Protocol method:

- (void) passUserInfo:(NSString *)jSonString
{
    userName.text = [[NSString alloc] initWithFormat:@"Welcome %@", jSonString];
}

and in the viewDidAppear method I call the loginSuccess method implemented in the LoginViewController class

-(void) viewDidAppear:(BOOL)animated{
protocolTest = [[LoginViewController alloc] init];
[protocolTest setDelegate:self];
[protocolTest loginSuccess];
}

loginSuccess method implemented in the LoginViewController class:

- (void)loginSuccess
{
    [[self delegate] passUserInfo:idJson];
}

and it should pass the idJson value to the HomeViewController (parent). The problem is that when I dismiss the modal view form, the idJson value is "NIL", so then in the HomeViewController I can't get this value. If I make this instead:

[[self delegate] passUserInfo:@"hello"];

I get the hello string in the HomeViewController (parent) What am I doing wrong??

Thanks in advance!!!


Your problem is that instead of using the existing LoginViewController that has the actual data. Your viewWillAppear is creating a new one that never made the connection and getting its empty data.

First in the app delegate, you need to set your HomeViewController (the one in the tabbar) as the delegate to the LoginViewController that you're presenting.

Then, from connectionDidFinishLoading: you should be calling the [delegate passUserInfo:idJson]; to inform the HomeVC that the login screen got data. You HomeVC's passUserInfo: method should probably dismiss the LoginVC with [self.tabBarController dismissModalViewControllerAnimated:YES]; (Since the login view was presented from the tabbar controller).

0

精彩评论

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