我试图从应用程序委托(delegate)弹出到导航堆栈的 Root View Controller ,但在将 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 将作为可选属性返回,因为窗口和 rootViewController 属性都是可选的。因此我们需要打开它以确保这些值确实存在
2) rootViewController 被定义为 UIViewController - 而不是 UINavigationController 子类。我们需要将其转换为 UINavigationController,以便能够将其作为一个整体进行操作。
尝试安全地将 Root View Controller 解包为 UINavigationController:
if let navigationController = self.window?.rootViewController as? UINavigationController {
navigationController.popToRootViewController(animated: true)
}
关于ios - 来自 appDelegate 的 popToRootViewController swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682278/