Mitch Cohen
Mitch Cohen

Reputation: 1645

How to popToRootViewController from within a modal view?

I have a typical UITableViewController set of views. On all views is a button, which brings up a model view. On that Settings view is a button. I would like that button to dismiss the modal view and implement popToRootViewController on the UITableViewController's navigationController.

Dismissing the modal view is easy:

[self dismissModalViewControllerAnimated:NO];

and that works fine. I've tried this to pop the main UITableViewController:

[self.parentViewController.navigationController popToRootViewControllerAnimated:NO];

and nothing happens.

I can probably implement a delegate to make this happen but there are quite a few view controllers with the same Settings button (with more to come) so a preference to find a solution that doesn't require additional code in each view controller.

Many thanks!

Upvotes: 5

Views: 5281

Answers (5)

Mohit Tomar
Mohit Tomar

Reputation: 5201

add a method in your appdelegate file

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

Now call it in your classs on ur logout button

[self dismissModalViewControllerAnimated:NO];
[(TestAppDelegate *)[[UIApplication sharedApplication] delegate]) GotoRoot];

please revert its result;

Upvotes: 1

tomi
tomi

Reputation: 96

check out the answer by rdelmal (https://stackoverflow.com/a/16311935/1395563), this worked like a charm for me. I use this code in an action in the modal view.

[(UINavigationController *)self.presentingViewController  popToRootViewController:NO];
[self dismissViewControllerAnimated:YES completion:nil];

Upvotes: 7

martinjbaker
martinjbaker

Reputation: 1484

This works for me

[[self parentViewController] popToRootViewControllerAnimated:NO]

Upvotes: 0

Mitch Cohen
Mitch Cohen

Reputation: 1645

Couldn't find a solution like this, so I've implemented a protocol/delegate which works fine. Just:

[self.navigationController popToRootViewControllerAnimated:NO];

in the delegate call.

Upvotes: 1

adam
adam

Reputation: 22587

Try

[[[[UIApplication sharedApplication].keyWindow] rootViewController] popToRootViewController:YES];

Upvotes: 1

Related Questions