如何使用Swift iOS向UIAlertView按钮添加动作

前端之家收集整理的这篇文章主要介绍了如何使用Swift iOS向UIAlertView按钮添加动作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想添加“确定”按钮以外的其他按钮,该按钮应该只是关闭警报.
我想要另一个按钮来调用某个功能.
var logInErrorAlert: UIAlertView = UIAlertView()
logInErrorAlert.title = "Ooops"
logInErrorAlert.message = "Unable to log in."
logInErrorAlert.addButtonWithTitle("Ok")

如何在此警报中添加另一个按钮,然后允许它在点击后调用一个函数,让我们说我们想要调用新按钮:

retry()
Swifty方式是使用新的UIAlertController和闭包:
// Create the alert controller
    let alertController = UIAlertController(title: "Title",message: "Message",preferredStyle: .Alert)

    // Create the actions
    let okAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default) {
        UIAlertAction in
        NSLog("OK Pressed")
    }
    let cancelAction = UIAlertAction(title: "Cancel",style: UIAlertActionStyle.Cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
    }

    // Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    // Present the controller
    self.presentViewController(alertController,animated: true,completion: nil)

斯威夫特3:

// Create the alert controller
    let alertController = UIAlertController(title: "Title",preferredStyle: .alert)

    // Create the actions
    let okAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.default) {
        UIAlertAction in
        NSLog("OK Pressed")
    }
    let cancelAction = UIAlertAction(title: "Cancel",style: UIAlertActionStyle.cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
    }

    // Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    // Present the controller
    self.present(alertController,completion: nil)
原文链接:/swift/320072.html

猜你在找的Swift相关文章