快速警报视图(iOS8)与确定和取消button,哪个button点击

我在Swift中编写了一个警告视图,我想确定用户select了哪个button(这是一个确认对话框),什么都不做或者执行。 目前我有:

@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() } 

我可能使用错误的button,请纠正我,因为这对我来说都是新的。

谢谢!

4 Solutions collect form web for “快速警报视图(iOS8)与确定和取消button,哪个button点击”

如果您使用的是iOS8,则应该使用UIAlertController – 不build议使用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) 

正如你可以看到button按下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) 
 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) 

您可以使用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显示警报

更新为swift 3:

//function定义:

 @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()函数definaiton:

 func logoutFun() { print("Logout Successfully...!") } 
  • 在Interface Builder中删除自动布局(约束)
  • Xcode项目不显示模拟器列表
  • XCode - 但是...我的档案在哪里?
  • 当使用Xcode 4.2和iOS 5 SDK时,是否有可能locking旧的iOS版本?
  • 如何在xcode中创buildDropDown?
  • RootViewController切换转换animation
  • iOS中的Crashlytics不会在Fabric应用程序中继续执行“Build Your Project”
  • iOS 10不打印NSLog
  • 无效Swift支持/无效执行的Swift
  • 从Info.plist中阅读版本
  • 在xcode中启用标签? 还是减轻没有他们的痛苦?