简体   繁体   English

如何使用 UIAlertController 显示警报

[英]How to present an alert with UIAlertController

I am trying to present an alert using swift.我正在尝试使用 swift 发出警报。 This is the code I used from viewDidLoad but nothing happens when the code is run.这是我从 viewDidLoad 使用的代码,但在运行代码时没有任何反应。 Can someone help?有人可以帮忙吗?

    var alert = UIAlertController(title: "test title",
        message: "test message",
        preferredStyle: .Alert)
    self.presentViewController(alert, animated: true, completion:nil)

You must present any view controller after its parent view has been appeared .您必须在其父视图出现后呈现任何视图控制器。 Put your presentation code to viewDidApear method.将您的演示文稿代码放入 viewDidApear 方法。

override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)

        var alert = UIAlertController(title: "test title",
            message: "test message",
            preferredStyle: .alert)
        self.present(alert, animated: true, completion:nil)

    }

Show AlerView in swift for ios8在 ios8 中快速显示 AlerView

func showAlertVIew(){

        var alert = UIAlertController(title: "Info", message: "Welcome to Swift world.", preferredStyle: UIAlertControllerStyle.Alert)

        let alertOKAction=UIAlertAction(title:"OK", style: UIAlertActionStyle.Default,handler: { action in
            println("OK Button Pressed")
            })

        let alertCancelAction=UIAlertAction(title:"Cancel", style: UIAlertActionStyle.Destructive,handler: { action in
            println("Cancel Button Pressed")
         })

        alert.addAction(alertOKAction)
        alert.addAction(alertCancelAction)

        self.presentViewController(alert, animated: true, completion: nil)
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从SKScene呈现UIAlertController - How to present UIAlertController from SKScene 当存在键盘时,使用 UITextView 向上移动 UIAlertController 样式警报 - move up UIAlertController style Alert with UITextView when keyboard is present react-native 警报错误“尝试呈现 UIAlertController - react-native alert error "Attemp to present UIAlertController iOS:UIAlertController上的textField成为当前警报的第一响应者 - iOS: textField on UIAlertController becoming first responder on alert present 在没有弹出窗口的情况下在iPad上显示UIAlertController(警报样式)是否安全? - Is it safe to present UIAlertController (alert style) on iPad without popover? 从AppDelegate到PresentedViewController的警报:“试图在…已经呈现UIAlertController上呈现UIAlertController” - Alert from AppDelegate to PresentedViewController: “Attempt to present UIAlertController on … whitch is already presenting UIAlertController” UIMenuController 和 UIAlertController 如何同时呈现? - How present UIMenuController and UIAlertController at the same time? Pythonista:如何显示UIAlertController? - Pythonista: How can I present a UIAlertController? 如何呈现位于另一个UIViewController中的UIAlertController - how to present UIAlertController located in another UIViewController iOS:如何在iCloud Singleton中显示UIAlertController? (迅速) - iOS: How to present UIAlertController in iCloud Singleton? (Swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM