带有“确定”和“取消”的快速警报视图:点击了哪个按钮?


105

我有一个用Swift编写的Xcode警报视图,我想确定用户选择了哪个按钮(这是一个确认对话框),该按钮什么也不做或执行某些操作。

目前我有:

@IBAction func pushedRefresh(sender: AnyObject) {
    var refreshAlert = UIAlertView()
    refreshAlert.title = "Refresh?"
    refreshAlert.message = "All data will be lost."
    refreshAlert.addButtonWithTitle("Cancel")
    refreshAlert.addButtonWithTitle("OK")
    refreshAlert.show()
}

我可能使用了错误的按钮,请对我进行纠正,因为这对我来说是全新的。


Answers:


303

如果您使用的是iOS8,则应该使用UIAlertController- 不推荐使用UIAlertView 。

这是一个如何使用它的示例:

var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
  print("Handle Ok logic here")
  }))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
  print("Handle Cancel Logic here")
  }))

presentViewController(refreshAlert, animated: true, completion: nil)

如您所见,UIAlertAction的块处理程序处理按钮的按下。这里有一个很棒的教程(尽管本教程不是使用swift编写的):http : //hayageek.com/uialertcontroller-example-ios/

Swift 3更新:

let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
    print("Handle Ok logic here")
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
    print("Handle Cancel Logic here")
}))

present(refreshAlert, animated: true, completion: nil)

Swift 5更新:

let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
      print("Handle Ok logic here")
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
      print("Handle Cancel Logic here")
}))

present(refreshAlert, animated: true, completion: nil)

4
您可以在示例中使用UIAlertActionStyle.Cancel而不是.Default
Tristan Warner-Smith

如果我不想在“取消”操作中做任何事情,我什么也不能返回?
加布里埃尔·罗德里格斯

当然,从技术上讲,除了记录外,我在示例中什么也没有做。但是,如果我删除日志,我将什么也不做。
Michael Wildermuth

1
更新较新的Swift版本的答案时真是太好了
BlackTigerX

任何人都知道如何将可访问性ID添加到“确定”和“取消”操作中
Kamaldeep singh Bhatia

18
var refreshAlert = UIAlertController(title: "Log Out", message: "Are You Sure to Log Out ? ", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in
    self.navigationController?.popToRootViewControllerAnimated(true)
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in

    refreshAlert .dismissViewControllerAnimated(true, completion: nil)


}))

presentViewController(refreshAlert, animated: true, completion: nil)

4

迅速更新3:

//函数定义:

@IBAction func showAlertDialog(_ sender: UIButton) {
        // Declare Alert
        let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure you want to Logout?", preferredStyle: .alert)

        // Create OK button with action handler
        let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
             print("Ok button click...")
             self.logoutFun()
        })

        // Create Cancel button with action handlder
        let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
            print("Cancel button click...")
        }

        //Add OK and Cancel button to dialog message
        dialogMessage.addAction(ok)
        dialogMessage.addAction(cancel)

        // Present dialog message to user
        self.present(dialogMessage, animated: true, completion: nil)
    }

// logoutFun()函数定义:

func logoutFun()
{
    print("Logout Successfully...!")
}

3

您可以使用UIAlertController轻松完成此操作

let alertController = UIAlertController(
       title: "Your title", message: "Your message", preferredStyle: .alert)
let defaultAction = UIAlertAction(
       title: "Close Alert", style: .default, handler: nil)
//you can add custom actions as well 
alertController.addAction(defaultAction)

present(alertController, animated: true, completion: nil)

参考:iOS Show Alert


0

您可能要考虑使用SCLAlertView,它是UIAlertViewUIAlertController的替代方法。

UIAlertController仅适用于iOS 8.x或更高版本,SCLAlertView是支持较早版本的不错选择。

github上查看详细信息

例:

let alertView = SCLAlertView()
alertView.addButton("First Button", target:self, selector:Selector("firstButton"))
alertView.addButton("Second Button") {
    print("Second button tapped")
}
alertView.showSuccess("Button View", subTitle: "This alert view has buttons")
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.