Question

I hope somebody could point me in the right direction.

I have 2 controllers, MasterViewController (which displays a lot of data) and LoginViewController (logs users in and gets that data).

The MasterViewController is the root controller. I have this code right at the top to check if the user is logged in. If they are logged in the loadView method is called, otherwise they are taken to the login view controller.

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDictionary *session = [Global getSession];
    if(!session){

        // Not logged in.
        LoginViewController *loginViewController = [LoginViewController alloc];
        [self.navigationController presentViewController:loginViewController animated:YES completion:nil];

    }else{
        [self loadView];
    }
}

Once they log in I have this code that takes them back to the MasterViewController with this code:

[self dismissViewControllerAnimated:NO completion:nil];

The problem is the MasterViewController is no loaded at all. How do I relad that entire view before the user is taken back to the view?

Thanks, Peter

Was it helpful?

Solution

There are different ways.

  1. Use delegate, and declare a method for reload in master view controller say 'reloadData'. Before presenting the login view controller, set the master view controller as a delegate for loginSuccessEvent. Once login is successful before dismissing it call the delegate method.

  2. Set a bool property, isReloadNeeded in app delegate. In master view controller, viewWillAppear or viewWillAppear check this bool to reload data. set the bool to yes from login view controller and reset after reload in master view controller.

To Do Option1:

Define a protocol for interaction

@protocol LoginDelegate

   -(void) loginSuccessPostProcess;

@end

In Login View Controller.

.h File --> Declare a property

@property (weak) id< LoginDelegate> loginDelegate;

.m --> After login success before dismissing, call the delegate method

//Login Success
[self. loginDelegate loginSuccessPostProcess];
[self dismissViewControllerAnimated:YES completion:nil];

Main view Controller -->Implement the method.

.h --> Adopt to protocol

  @interface mainViewCOntroller < LoginDelegate>

.m

  1. Set Main view Controller as a delegate to the delegate property in Login View Contoller after instantiation and before presenting it

    loginViewController *login = [[LoginViewController alloc] init]; //Set your delegate after instantiation and before presenting login.loginDelegate = self; [self presentViewController:login];

Implement the method for reload

 -(void) loginSuccessPostProcess
 {
        //Your Method.
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top