【问题标题】:How to add action to buttons in Alert in Swift如何在 Swift 的警报中为按钮添加操作
【发布时间】:2014-07-04 23:42:47
【问题描述】:
我是初级程序员。我正在尝试向警报按钮添加操作,但它不起作用。我只是想测试一下警报按钮选择是否可以更改标签中的文本,但它不起作用。当然我可以很好地看到警报和按钮,但是单击按钮后没有任何反应。
@IBOutlet var statusLabelAlert : UILabel
var alertTest = UIAlertView()
@IBAction func alertButton(sender : AnyObject) {
alertTest.message = "Select one!"
alertTest.addButtonWithTitle("1st")
alertTest.addButtonWithTitle("2nd")
alertTest.addButtonWithTitle("3rd")
alertTest.title = "Test Alert"
alertTest.show()
}
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){
switch buttonIndex{
case 0:
statusLabelAlert.text = "1st"
case 1:
statusLabelAlert.text = "2nd"
case 2:
statusLabelAlert.text = "3rd"
default:
statusLabelAlert.text = "error"
}
}
【问题讨论】:
标签:
ios
swift
uialertview
uialertviewdelegate
【解决方案1】:
您的 clickedButtonAtIndex 方法是否在调用?设置断点并调试代码。
你没有设置 alertView 的委托。
@IBOutlet var statusLabelAlert : UILabel
var alertTest = UIAlertView()
alertTest.delegate = self //set the delegate of alertView
@IBAction func alertButton(sender : AnyObject) {
alertTest.message = "Select one!"
alertTest.addButtonWithTitle("1st")
alertTest.addButtonWithTitle("2nd")
alertTest.addButtonWithTitle("3rd")
alertTest.title = "Test Alert"
alertTest.show()
}
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){
switch buttonIndex{
case 0:
statusLabelAlert.text = "1st"
case 1:
statusLabelAlert.text = "2nd"
case 2:
statusLabelAlert.text = "3rd"
default:
statusLabelAlert.text = "error"
}
}
【解决方案2】:
您必须设置警报视图的委托:
alertTest.delegate = self
UIAlertView 使用delegate pattern,这意味着它调用其委托上的方法来实现某些事情或通知事件。对于UIAlertView,其中一个事件是用户点击按钮时。要让警报视图知道向谁报告用户的点击,您必须指定一个实现UIAlertViewDelegate 协议的委托。您的代码实现了协议,但它从未告诉警报您想成为它的委托人,因此您永远不会收到方法调用。
【解决方案3】:
@IBOutlet var statusLabelAlert : UILabel
var alertTest = UIAlertView()
@IBAction func alertButton(sender : AnyObject)
{
alertTest.delegate = self
alertTest.message = "Select one!"
alertTest.addButtonWithTitle("1st")
alertTest.addButtonWithTitle("2nd")
alertTest.addButtonWithTitle("3rd")
alertTest.title = "Test Alert"
alertTest.show()
}
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
{
switch buttonIndex
{
case 0:
statusLabelAlert.text = "1st"
case 1:
statusLabelAlert.text = "2nd"
case 2:
statusLabelAlert.text = "3rd"
default:
statusLabelAlert.text = "error"
}
}
【解决方案4】:
IBOutlet var statusLabelAlert : UILabel
@IBAction func alertButton(sender : AnyObject) {
let alert = UIAlertController(title: "Alert", message: "This is an Alert", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "1st", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
statusLabelAlert.text = "1st"
}))
alert.addAction(UIAlertAction(title: "2nd", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
statusLabelAlert.text = "2nd"
}))
self.presentViewController(alert, animated: true, completion: nil)
}
如果你不想在按下按钮时做某事:
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
【解决方案5】:
如果这适用于 iOS 8,您应该切换到 UIAlertController 和 UIAlertAction。 UIAlertAction 包含按钮应该做什么。