OGeek|极客世界-中国程序员成长平台

标题: iOS 回根 ViewController 在 modal 和导航 uiviewcontroller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:42
标题: iOS 回根 ViewController 在 modal 和导航 uiviewcontroller

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

当我断开蓝牙时,我需要回到 Root 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:模态视图 Controller 显示,

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

但是dismiss modal viewcontroller后并没有回到根导航 Controller 。

案例2:就在uinavigation Controller 页面中。

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

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

非常感谢。

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

把代码改成

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

     [self performSelectorselector(gotoRoot) withObject:nil afterDelay:0.50];

 - (void) gotoRoot {

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

     [myNavCon popToRootViewControllerAnimated:YES];
 }



Best Answer-推荐答案


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

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

- (void)gotoRoot {

    [self.navigationController popToRootViewControllerAnimated:NO];
}

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4