【问题标题】:iOS back to root ViewController in the modal and navigation uiviewcontrolleriOS 回根 ViewController 在 modal 和导航 uiviewcontroller
【发布时间】:2015-04-21 05:50:29
【问题描述】:

我有一个关于导航的结构,并且许多页面在 uiviewcontroller(UINavigationController) 上有模式(弹出)。

当我断开蓝牙时,我需要回到根视图控制器。

所以我在disconnect方法中设置了dismiss和popToRoot

 -(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];
 ....
 }

但是当我运行程序并断开蓝牙时,

在情况1:模式视图控制器显示,

它将关闭模态视图控制器,关闭是正确的。

但是dismiss modal viewcontroller后并没有回到根导航控制器。

案例2:就在uinavigation控制器页面中。

当我断开蓝牙时,没有回到根导航控制器。

我怎样才能回到导航根页面?我的失败在哪里?

非常感谢。

// ------ 答案-------

把代码改成

  [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];
 }

【问题讨论】:

    标签: ios objective-c uiviewcontroller modalviewcontroller navigationcontroller


    【解决方案1】:

    从您呈现模态视图的类中调用模态的解除,然后在延迟一段时间后执行选择器,然后执行这里的示例代码

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

    【讨论】:

    • 我测试了代码,navigationController popToRootViewControllerAnimated 还是不行。([appDelegate.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];)
    • 我不能使用 [appDelegate.window.rootViewController.navigationController popToRootViewControllerAnimated:YES]; ?
    • 在控制器中更好地使用 [self.navigationController popToRootViewControllerAnimated:NO]。
    • 类中的断开功能没有扩展uiviewcontroller。所以我需要使用 appdelegate 来获取当前的 viewcontroller 和 poptoRoot。所以我需要使用appdelegate来获取navigationController。我不能使用 [self.navigationcontroller popToRootViewControllerAnimated:NO]。
    • 添加 appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];内部断开方法
    【解决方案2】:

    来自apple developer documentation关于dismissViewControllerAnimated:completion:

    completion:视图控制器关闭后要执行的块。这个块没有返回值,也没有参数。您可以为此参数指定 nil。

    所以我认为这是更好的解决方案

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

    使用completion 块优于afterDelay。你如何选择好的延迟?如果太短会怎样?如果太长,执行代码什么都不等……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多