ios - Swift – 从 AppDelegate 向 View Controller 添加子视图
问题描述
我正在尝试从AppDelegate
当前显示添加子视图UIViewController
,我注意到如果在添加子视图UIViewController
时显示警报消息AppDelegate
,则子视图将添加到UIAlertController
而不是UIViewController
. UIViewController
显示子视图而不是当前UIViewController
可能显示的任何其他子视图的最佳方式是什么?这是我当前的代码。
@objc func showStudentLeftTheTestAlert(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let visibleVc = self.window?.visibleViewController {
// do some other stuff here
visibleVc.view.addSubview(StudentLeftTestAlertViewController.sharedInstance.view)
}
}
}
解决方案
问题在于,警报消息实际上是以UIViewController
模态方式呈现在您的原始UIViewController
. 如果你想到达顶shown
视图控制器(不是presented
),你可以添加这个方法给你AppDelegate
。如果您使用导航堆栈,它将返回您的根视图控制器或顶部视图控制器。
func getTopViewController() -> UIViewController? {
let rootViewController = self.window?.rootViewController
if let navigationController = rootViewController as? UINavigationController {
// get the top view controller from stack if needed
return navigationController.topViewController
} else {
return rootViewController
}
}
希望对你有帮助
推荐阅读
- javascript - 正确使用 Express 的 res.render() 和 res.redirect()
- c++ - 如何验证集合C ++中是否存在元素< x
- sql - SQL Case MAX WHEN LIKE
- amazon-ec2 - 列出标签值 - EC2 Boto3
- c# - C# - Visual Studio 建议我将属性重命名为已经存在的其他内容
- bash - bash source 一个结尾有退出命令的文件
- javascript - javascript中的DOT.files?这是什么意思?
- javascript - AWS Lambda & API Gateway - 来自浏览器的 500 但不是来自 curl
- ios - 关闭一个自定义弹出 UIViewController 并立即呈现另一个自定义弹出 UIViewController -SWIFT
- mongodb - 当我在终端中输入 mongod 时,我的 MongoDB 不工作