我在用Swift编写的Xcode中有一个alert 视图,我想确定用户 Select 了哪个按钮(这是一个确认对话框)不执行任何操作或执行某些操作.

目前我有:

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

我可能是用错按钮了,请纠正我,因为这对我来说是全新的.

推荐答案

如果您使用的是iOS8,那么应该使用UIAlertController-UIAlertView为deprecated.

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

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编写的):

Swift 3 update:

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 update:

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.3 update:

let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertController.Style.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相关问答推荐

{Singleton类}类型的值没有成员$showegView'

数据不会被ARC删除在swift'

如何更新Square Order?

为什么我的引用类型对象在初始化后有3个强引用?

变量捕获:变量在函数闭包中的行为

如何在AudioKit中实现自定义音效 node ?

通常从数组调用的SWIFT静态协议函数

为SwiftUI文本视图提供固定宽度,并仅在文本换行时使文本视图底部增大

可选,类似于自定义类型的初始化

协议元类型'SomeProtocol.Type'上无法使用静态成员'currency'

如何为等待函数调用添加超时

MacOS KIND 是如何实现的

从 Swift 列表中的行中检索值

NSFontAttributeName 已更改为 String

Swift 4 Decodable - 以枚举为键的字典

如何从 Button 获取标签名称?

为什么我可以转换为 NSManagedObject 但不能转换为我的实体类型?

Swift 3:日期与 NSDate?

显示 UIAlertController 的简单 App Delegate 方法(在 Swift 中)

tableView 部分标题消失 SWIFT