如何在呈现控制器的顶部显示 UIAlertController
How to show UIAlertController top of the presenting cotroller
我有顶视图控制器正在显示,我想显示警报控制器但它没有显示。有什么办法吗?
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
检查这个:
func showAlert() {
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
let yesAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
print("YES tapped")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action: UIAlertAction!) -> Void in
print("Cancel tapped")
}
alert.addAction(yesAction)
alert.addAction(cancelAction)
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
还请检查并确保您的 rootViewController 不为零。
终于解决了我的问题。很简单,只需将我们站立的控制器输入下面的 show alertcontroller 函数即可:
func showAlerControler(title: String?, message: String, confirmButtonText: String?, cancelButtonText: String?, atController: UIViewController){
var alert: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
if let confirmStr = confirmButtonText{
alert.addAction(UIAlertAction(title: confirmStr, style: .Default, handler: { (ACTION) -> Void in
}))
}
if let cancelStr = cancelButtonText{
alert.addAction(UIAlertAction(title: cancelStr, style: .Default, handler: { (ACTION) -> Void in
}))
}
atController.presentViewController(alert, animated: true, completion: nil)
}
不要使用appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
并且你可以站在任何控制器上调用showAlerControler并输入atController是self
(Objective-C)
UIAlertController *alertVC = [UIAlertController
alertControllerWithTitle:@"myTitle" message:@"myMessage"
preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
[alertVC dismissViewControllerAnimated:YES completion:nil];
}
]];
[self presentViewController:alertVC animated:YES completion:nil];
我有顶视图控制器正在显示,我想显示警报控制器但它没有显示。有什么办法吗?
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
检查这个:
func showAlert() {
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
let yesAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
print("YES tapped")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action: UIAlertAction!) -> Void in
print("Cancel tapped")
}
alert.addAction(yesAction)
alert.addAction(cancelAction)
var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
还请检查并确保您的 rootViewController 不为零。
终于解决了我的问题。很简单,只需将我们站立的控制器输入下面的 show alertcontroller 函数即可:
func showAlerControler(title: String?, message: String, confirmButtonText: String?, cancelButtonText: String?, atController: UIViewController){
var alert: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
if let confirmStr = confirmButtonText{
alert.addAction(UIAlertAction(title: confirmStr, style: .Default, handler: { (ACTION) -> Void in
}))
}
if let cancelStr = cancelButtonText{
alert.addAction(UIAlertAction(title: cancelStr, style: .Default, handler: { (ACTION) -> Void in
}))
}
atController.presentViewController(alert, animated: true, completion: nil)
}
不要使用appDelegate.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
并且你可以站在任何控制器上调用showAlerControler并输入atController是self
(Objective-C)
UIAlertController *alertVC = [UIAlertController
alertControllerWithTitle:@"myTitle" message:@"myMessage"
preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction* action) {
[alertVC dismissViewControllerAnimated:YES completion:nil];
}
]];
[self presentViewController:alertVC animated:YES completion:nil];