Ios 在ViewController外部显示UIAlertController
我很难显示UIAlertController,因为我试图在一个不是ViewController的类中显示它 我已经尝试添加它:Ios 在ViewController外部显示UIAlertController,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我很难显示UIAlertController,因为我试图在一个不是ViewController的类中显示它 我已经尝试添加它: var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewControll
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
这是行不通的。。。
我还没有找到适合我的解决方案 创建从当前视图控制器调用的辅助函数,并将当前视图控制器作为参数传递:
func showAlertInVC(
viewController: UIViewController,
title: String,
message: String)
{
//Code to create an alert controller and display it in viewController
}
如果您的解决方案不起作用,可能是因为当时没有窗口。当我试图在
应用程序中显示警报视图时,我遇到了同样的问题:DidFinishLoadingWithOptions
方法。在本例中,我的解决方案是检查根视图控制器是否可用,如果不可用,则为uiapplicationdibecomeactivityfication
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification,
object: nil,
queue: NSOperationQueue.mainQueue()) {
(_) in
//show your alert by using root view controller
//remove self from observing
}
}
我在
UIAlertController
上写了这个extension
来恢复show()
它使用递归查找当前俯视图控制器:
extension UIAlertController {
func show() {
present(animated: true, completion: nil)
}
func present(animated: Bool, completion: (() -> Void)?) {
if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
presentFromController(controller: rootVC, animated: animated, completion: completion)
}
}
private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
if
let navVC = controller as? UINavigationController,
let visibleVC = navVC.visibleViewController
{
presentFromController(controller: visibleVC, animated: animated, completion: completion)
} else if
let tabVC = controller as? UITabBarController,
let selectedVC = tabVC.selectedViewController
{
presentFromController(controller: selectedVC, animated: animated, completion: completion)
} else if let presented = controller.presentedViewController {
presentFromController(controller: presented, animated: animated, completion: completion)
} else {
controller.present(self, animated: animated, completion: completion);
}
}
}
现在,它非常简单:
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
alertController.show()
这应该行得通
UIApplication.sharedApplication().windows[0].rootViewController?.presentViewController(...)
太好了!非常感谢,很高兴它能为您工作,我已经在许多VC场景中使用过它(选项卡栏控制器内导航堆栈上的模态VC等),如果出现这种情况,请让我知道……SharedApplication不能用于应用程序扩展。如果key view controller已以模式显示某些内容,则这种情况不起作用。我必须添加另一个检查
如果let presented=controller.presentedViewController{presentFromController(controller:presented,animated:animated,completion:completion)}否则{controller.present(self,animated:animated,completion:completion)}
您是一个不是UIViewController的类,现在部分是不稳定的。考虑添加委托或基于块的回调,以便在使用该类的视图控制器上显示警报。