2

I have a structure about navigation and many page have modal (popup) on the uiviewcontroller(UINavigationController).

When I disconnect the bluetooth, I need back to the root the viewcontroller.

So I set the dismiss and popToRoot in the disconnect method

 -(void) disconnect
 {
 ....
  [appDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

  NSLog(@"appDelegate.window.rootViewController:%@",appDelegate.window.rootViewController.class);
 // show log appDelegate.window.rootViewController:UINavigationController

  [appDelegate.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];
 ....
 }

But when I run the program and disconnect bluetooth,

In the case 1: modal the viewcontroller showing,

It will dismiss the modal viewcontroller, the dismiss was correct.

But there are not back to the root navigation controller after dismiss modal viewcontroller.

In the case2: just in the uinavigation controller page.

when I disconnect the bluetooth, there are not back to the root navigation controller.

How can I back to the navigation root page?where are my fails?

thank you very much.

// ------ answer -------

change code to

  [appDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

     [self performSelector:@selector(gotoRoot) withObject:nil afterDelay:0.50];

 - (void) gotoRoot {

     UINavigationController *myNavCon = (UINavigationController*)appDelegate.window.rootViewController;

     [myNavCon popToRootViewControllerAnimated:YES];
 }
dickfala
  • 3,246
  • 3
  • 31
  • 52

2 Answers2

4

From the class you presented your modal view call dismiss of modal and then perform selector after some delay and then do the here is the sample code

- (void) dismissAndGoToRoot {
      [self dismissViewControllerAnimated:YES completion:nil];
      [self performSelector:@selector(gotoRoot) withObject:nil afterDelay:0.50];
}

- (void)gotoRoot {

    [self.navigationController popToRootViewControllerAnimated:NO];
}
Tanuj
  • 531
  • 4
  • 10
  • I test the code, the navigationController popToRootViewControllerAnimated still not working.([appDelegate.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];) – dickfala Apr 21 '15 at 06:02
  • Can't I use [appDelegate.window.rootViewController.navigationController popToRootViewControllerAnimated:YES]; ? – dickfala Apr 21 '15 at 06:12
  • better use [self.navigationController popToRootViewControllerAnimated:NO] inside your controller. – Tanuj Apr 21 '15 at 06:14
  • The disconnect function in the class not extend uiviewcontroller. So I need use appdelegate to get the present viewcontroller and poptoRoot. So I need use the appdelegate to get the navigationController. I can't use [self.navigationcontroller popToRootViewControllerAnimated:NO]. – dickfala Apr 21 '15 at 06:16
  • add appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; inside disconnect method – Tanuj Apr 21 '15 at 06:21
0

From apple developer documentation about dismissViewControllerAnimated:completion:

completion: The block to execute after the view controller is dismissed. This block has no return value and takes no parameters. You may specify nil for this parameter.

So I think this is better solution

[self dismissViewControllerAnimated:YES completion:^(){
    [self.navigationController popToRootViewControllerAnimated:NO];
}];

Using completion block is better than afterDelay. How do you choose the good delay ? What happens if too short ? If too long, execution code waits for nothing ...

koen
  • 5,383
  • 7
  • 50
  • 89
europrimus
  • 53
  • 5