iOS 回到模态和导航 uiviewcontroller 中的根 ViewController

标签 ios objective-c uiviewcontroller modalviewcontroller navigationcontroller

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

当我断开蓝牙连接时,我需要返回到 View Controller 的根目录。

所以我在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 中: View Controller 显示模态,

它将关闭模态视图 Controller ,关闭是正确的。

但是在关闭模态视图 Controller 后没有回到根导航 Controller 。

在情况2中:就在uinavigation Controller 页面中。

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

如何回到导航根页面?我的失败在哪里?

非常感谢。

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

将代码更改为

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

最佳答案

从您提出模态视图的类中调用模态的关闭,然后在延迟一段时间后执行选择器,然后执行以下操作,这是示例代码

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

- (void)gotoRoot {

    [self.navigationController popToRootViewControllerAnimated:NO];
}

关于iOS 回到模态和导航 uiviewcontroller 中的根 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763952/

相关文章:

objective-c - 从类的方法显示 UILabel

ios - UIButton 的动画背景颜色从白色变为红色

iphone - 有没有办法动态更改 View 以填充屏幕?

ios - 较小尺寸的 uiviewcontroller

ios - 在 Core Animation 中使用贝塞尔曲线的工作流程

ios - 将表的行数设置为特定实体

ios - 通过在 iOS 中安装 Gmail 应用凭据登录 Google 帐户

ios - For循环不改变变量

ios - 在自定义 segue 过渡期间, View 的导航栏项目放错了位置

ios - 在同一文件中的 View Controller 之间以 Swift 传递数据