ios - 来自 appDelegate 的 popToRootViewController swift

标签 ios objective-c swift

我试图从应用程序委托(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/

相关文章:

ios - Socket.IO-Client-Swift 无法连接到nodejs socket.io 服务器

ios - WatchOS 应用程序未检测到配套的 iOS 应用程序

html - 获取 HTML 形式的文本文件的正确格式

objective-c - 在 objective-c 中导入 header

iphone - NSAttributedString 能否用于将 native 应用程序操作与文字联系起来?

ios - 当我点击文本字段时,它们没有响应!就好像它们粘在一起了还是什么?

arrays - Swift:代码由于某种原因崩溃

ios - j__objc_msgSend 和 objc_msgSend 有什么区别?

ios - API Gateway 为授权用户使用 Cognito 用户池生成了适用于 iOS (Objective-C) 的 SDK

ios - NSFileHandle writeData 同步延迟主线程操作