YYfim
YYfim

Reputation: 1412

ViewController unwanted allocation and initialised

I'm having some trouble understanding why my viewController gets allocated a second time. the overall idea is this:

1) i have a first viewController (called loginScreenViewController) in this viewController the user selects login from FB/T/Mail

2)once the user selects the mail option i push the next viewController called mailRegistrationViewController

3)once the user logs/reg with this viewController i segue to the next viewController called homeViewController and pop the mailRegistrationViewController of the navigation stack

pop

NSMutableArray *navStack = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[navStack removeObjectIdenticalTo:self];
[self.navigationController setViewControllers:navStack];

navigation stack after removing mailRegistration:

"<SELoginScreenViewController: 0x8f46030>",
"<SEHomeViewController: 0x8e62dc0>"

4) in the homeViewController once the user logs out, i log him out and pop the homeViewController, this time with

[self.navigationController popViewControllerAnimated:YES]; // remove from the navigation stack

once again i print the navigation stack before and after the removing

before:

"<SELoginScreenViewController: 0x8f46030>",
"<SEHomeViewController: 0x8e62dc0>"

after:

"<SELoginScreenViewController: 0x8f46030>"

Then in the loginViewController in viewWillAppear i log the navigation stack and this is what i get:

"<SELoginScreenViewController: 0x8f46030>",
"<SELoginScreenViewController: 0x8f6ea70>"

My login view gets allocated and initialised once more. If any has an idea about why this is happening, or if i need to add more code please let me know.

Thanks

Upvotes: 1

Views: 67

Answers (1)

Vinod Vishwanath
Vinod Vishwanath

Reputation: 5891

Ideally, you should only use pushViewController:animated and popViewController:animated to alter the viewController stack of a UINavigationController.

Perform step 3 like this:

    do
    {
    id *poppedVC = [self.navigationController popViewControllerAnimated:NO]; 
            // animation not required since you will soon push a new view controller with animated:YES

    } while(poppedVC != self);


    [self.navigationController pushViewController:homeViewController animated:YES];

Basically this changes the order in which you remove the mailRegistrationViewController from your viewController stack.

Upvotes: 1

Related Questions