开发者

How to pop UINavigationController to the caller

I have a UIViewController that is not on the navigation stack. It initialises another UIViewController which is on the top of the stack. The new root controller pushes more controllers on the stack and so on.

Now, I will need to pop all controllers down back to the controller which started the process. However, popToRootViewControllerAnimated doesn't work.

ie:开发者_高级运维 A -> B -> C -> D where B, C and D are on the navigation stack and I need to go back to controller A from D. How to do it?

-(void) loadScreenWithNavigation:(UIViewController *)controller
{    
    _navController = [[UINavigationController alloc] initWithRootViewController:(_loadedController = controller)];
    _navController.navigationBar.barStyle = UIBarStyleBlack;
    _navController.navigationBar.translucent = NO;

    [self.view addSubview:[_navController view]];
    [self.view bringSubviewToFront:[_navController view]];
}

That's how I started the navigation stack in my "home" controller. I need to come back to the home controller.


Use standard UINavigationController's method:

[self.navigationController popToRootViewControllerAnimated:YES];

Then, you can removeFromSuperview your custom initialized view.


In AppDelegate,

UINavigationController *navController;

in AppDelegate.m file,

self.navController=[[UINavigationController alloc] initWithRootViewController:yourViewController];

[self.window addSubview:self.navController.view];

when you push yourViewcontrollerA to viewcontrollerB ,

[self.navigationController viewcontrollerB animated:YES];

same for viewControllerB to C and C to D ..

now if You want to go back D to C then,

[self.navigationController popToRootViewControllerAnimated:YES];


I suppose you want to remove this [_navController view] right? which you can do as follows :

 if([_navController.view superview])
        [_navController.view removeFromSuperview];

EDIT:

In a simplified way, any UIViewController will disappear in the same fashion it appeared. If any view was added as

1.) addSubView method :

[self.view addSubView: myView];

it would be removed by

[myView removeFromSuperView];

2.) stack it to navigationController

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

this view will pop out of stack as

[self.navigationController popViewControllerAnimated:YES];

3.) present modal view

[self presentModalViewController:objctOfYourViewController animated:NO];

which will be removed as

[self dismissModalViewControllerAnimated:YES];

Now, just in the way you added your views, you should make them remove.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜