I am trying to follow this question to setup a delegate for a login sequence:- How do I set up a simple delegate to communicate between two view controllers?
In my application I have a Main View (MESHomeViewController). I also have a login structure as follows:
- MESLoginNavController
- With a root view controller of MESWelcomeViewController
- There are also MESLoginViewController and MESSignupViewControllers which can be pushed to the stack.
My aim is to have a delegate method which is only called when the user logs in. I am using a delegate as I cannot use viewDidLoad as the view is often reloaded/shown on different occasions so I need to see when the user has completed the login.
The user can login via the Welcome controller (via Facebook) or the login controller (via normal methods) and via the Signup (via signup ;).
This is what I have tried to implement below: MESHomeViewController.h
#import "MESWelcomeViewController.h"
@interface MESHomeViewController : UIViewController <LoginViewControllerDelegate>
@end
In the MESHomeViewController.m I check if the user is logged in, if they are not complete the following:
NSLog(@"not logged in");
MESWelcomeViewController *loginNavVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WelcomeVC"];
loginNavVC.delegate = self;
[self presentModalViewController:loginNavVC animated:NO];
In the MESWelcomeViewController.h I have the following:
@protocol LoginViewControllerDelegate;
@interface MESWelcomeViewController : UIViewController <NSURLConnectionDataDelegate>
@property (nonatomic, weak) id<LoginViewControllerDelegate> delegate;
@end
@protocol LoginViewControllerDelegate <NSObject>
- (void)didLoginUser;
@end
Once the user is completely logged in via a custom method. MESWelcomeViewController.m once user logged in
NSLog(@"%@",self.delegate);
if ([self.delegate respondsToSelector:@selector(didLoginUser)]) {
[self.delegate didLoginUser];
}
However, the delegate method is not being called, it appears the self.delegate is Null for the welcomeViewController. I am thinking possibly I should set the delegate as the Login Nav Controller based on the above setup, but then I am not sure how to call the delegate method from a view controller currently pushed on the nav?