快速从 appDelegate 中 popToRootViewController

2024-05-30

我试图从应用程序委托弹出到导航堆栈的根视图控制器,并且在将 obj-c 中的内容转换为 swift 时遇到一些问题。

obj-c 中的工作原理:

UINavigationController *navigationController = (UINavigationController  *)self.window.rootViewController;
[navigationController popToRootViewControllerAnimated:YES];

我尝试转换:

  var navigationController: UINavigationController = self.window?.rootViewController;
        navigationController.popToRootViewControllerAnimated(true);

我收到一条错误消息说 UIViewController?不可转换为 UINavigationController'

我怎样才能解决这个问题?


这里有一些事情在起作用:

1) rootViewController 将作为可选属性返回,因为 window 和 rootViewController 属性都是可选的。因此我们需要打开它以确保这些值确实存在

2) rootViewController 被定义为 UIViewController - 而不是 UINavigationController 子类。我们需要将其转换为 UINavigationController 以便能够将其作为一个整体进行操作。

尝试安全地将根视图控制器解包为 UINavigationController:

if let navigationController = self.window?.rootViewController as? UINavigationController {
        navigationController.popToRootViewController(animated: true)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

快速从 appDelegate 中 popToRootViewController 的相关文章

随机推荐