开发者

iOS - Weird navigation problem?

开发者 https://www.devze.com 2023-03-28 10:55 出处:网络
I have 3 UIViewControllers in my UINavigationController. At some points I want to go to rootViewController and from there navigate to a new UIViewController, and it doesn\'t aeem to work.

I have 3 UIViewControllers in my UINavigationController. At some points I want to go to rootViewController and from there navigate to a new UIViewController, and it doesn't aeem to work.

Any suggestions?

- (IBAction)goToRootAndNavigateToViewController
{
   [self.navigationController popToRootViewControllerAnimated:YES];

   MyViewController *mvc = [[MyViewController alloc] init];
   [self.navigationController pushViewController:mvc animated:YES];
   [mvc release];

   //This takes me to the rootViewController but it doesn't navigate to MyViewController
}

Trying to use performSelector:WithDelay:

- (void)goToRootAndNavigateToViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];

    [self performSelector:@selector(doSomething) withObject:nil afterDelay:10];
}

- (void)doSomething
{
    MyViewController *mvc = [[MyViewController alloc] init];
    [self.navigationController pushViewContro开发者_开发问答ller:mvc animated:YES];
    [mvc release];
}


I believe the popToRootViewController takes up the full NSRunLoop.

You would need to push your next view controller with a separate function using something like performSelector:withObject:afterDelay.

or you could always just do a [self.navigationController setViewControllers:] call to set them manually


You need to push your mvc controller when the animation is complete. Try calling it once the first animation is done (e.g. in - (void)viewDidAppear:(BOOL)animated)


I don't know why you are facing this problem, but one solution you could try is to push the new view controller in your root view controller's -viewDidAppear: method.


I guess it has to do with your current viewcontroller using - (IBAction)goToRootAndNavigateToViewController, is somehow losing its control once being popped. Thus, making consecutive statements to be not working.

If I were you, I would make sure pushing MyViewController instance is done always at rootViewController of your choice, not from the current viewcontroller which is going to be popped from UINavigationController and possibly to be released and deallocated.

Probably, you may want to add delegate method implementation for, such as - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated in your rootViewController

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // Check the right condition for pushing MyViewController...
    // if it's YES...
    MyViewController *mvc = [[MyViewController alloc] init];
   [self.navigationController pushViewController:mvc animated:YES];
   [mvc release];
}

In this implementation, you may push MyViewController instance. One thing you have to do beforehand is using some kind of conditional flag, which will make sure the situation is correct for popToRootViewController then push MyViewController

0

精彩评论

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