我是初级程序员。我正在尝试向Alert上的按钮添加操作,但它不起作用。我只想测试警报按钮选项是否可以更改标签中的文本,但它不起作用。当然,我可以很好地看到警报和按钮,但点击按钮后没有任何反应。
@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"
}
}
答案 0 :(得分:9)
你的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"
}
}
答案 1 :(得分:3)
您必须设置警报视图的代理:
alertTest.delegate = self
UIAlertView
使用delegate pattern,这意味着它调用其委托上的方法来实现某些事情或通知事件。对于UIAlertView
,其中一个事件是用户点击按钮时。要使警报视图知道要告知用户的点击,您必须指定实现UIAlertViewDelegate
协议的委托。您的代码实现了协议,但它从不告诉警报您希望成为其委托,因此您永远不会收到方法调用。
答案 2 :(得分: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"
}
}
答案 3 :(得分:1)
如果是iOS 8,你应该切换到UIAlertController和UIAlertAction。 UIAlertAction包含按钮应该做的事情。
答案 4 :(得分:1)
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))