0

i am new to IOS . My question is, i have some view-controllers as NavigationController,mainVC, VC1, VC2, VC3, CameraVC. In cameraVC i have a done button having action doneClicked. These all View-Controller are pushed in NavigationController. VC1 is presented, not pushed in nav-controller. doneClicked function implemented poptorootviewcontroller. when i click on done button it let me to VC1 but not the mainVC. Is there any way so i can pop all view controller to VC1 and after this automatically dismiss VC1 to mainVC.

Zeebok
  • 388
  • 1
  • 3
  • 15

3 Answers3

0

Make your MainVC as root view controller and in the IBAction of done button use the code to pop to MainVC.

[self.navigationController popToRootViewControllerAnimated:YES];

Hope it helps.

iPeter
  • 1,276
  • 9
  • 25
  • Thanks Peter for your response. Actually my mainVC is added in TabBarController so i can not make mainVC as root view controller. Is there any way to make protocol/delegate to perform some action as well as to dismiss view controller. – Zeebok Apr 01 '15 at 12:50
  • You can refer to [this](http://stackoverflow.com/questions/14636891/dismissing-a-presented-view-controller) – iPeter Apr 01 '15 at 12:58
0

To pop view controllers

[self.navigationController popToRootViewControllerAnimated:YES];

and to dismiss presented view controller

[self dismissViewControllerAnimated:NO completion:nil]

0
[self.navigationController setViewControllers:@[mainVC]];

I think this code will work on your situation. iOS Developer Library:

Replaces the view controllers currently managed by the navigation controller with the specified items.

- (void)setViewControllers:(NSArray *)viewControllers
              animated:(BOOL)animated

source

you must do this after dissmiss present view controller. Use delegate

Now let's think your navigation stack is empty and your root is mainVC. you want to present VC1 that's ok just present it. But you should give a delegate to mainVC to what's gonna happen after dissmissing VC1. For example you present VC1 from mainVC. And you want to push VC2 after dissmiss VC1. That's ok just in mainVC has a delegate so in this method

[self.navigationController pushViewController:VC2];

Present views does not affect your navigation stack. It's not in your stack. so everytime you dismiss it from a controller you should give an delegate to that controller to what will happen after dissmissing.

Try it. Hope it helps.

Gürhan KODALAK
  • 549
  • 7
  • 20
  • how can i use delegate while i am on 4th level.i pushed 1vc then 2nd then 3rd then 4th. . from 4th level is there any way to implement protocol. – Zeebok Apr 01 '15 at 13:28
  • yes there is way but I should know how your navigation map is. Let me edit my answer to explain a scenario. I'm gonna show a small example. – Gürhan KODALAK Apr 01 '15 at 13:34
  • @Zeebok, I think you can use delegate to achieve it. I use Notification to popToRootVC, and Dismiss , works well. – dengApro Aug 12 '17 at 11:09