加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:46:12 所属栏目:百科 来源:网络整理
导读:我想添加“确定”按钮以外的其他按钮,该按钮应该只是关闭警报. 我想要另一个按钮来调用某个功能. var logInErrorAlert: UIAlertView = UIAlertView()logInErrorAlert.title = "Ooops"logInErrorAlert.message = "Unable to log in."logInErrorAlert.addButto
我想添加“确定”按钮以外的其他按钮,该按钮应该只是关闭警报.
我想要另一个按钮来调用某个功能.
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读