我是初级程序员.我正在尝试向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" } }
解决方法
你的clickedButtonAtIndex方法是否正在调用?设置断点并调试代码.
您没有设置alertView的委托.
您没有设置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" } }