7

I am beginner level programmer. I am trying to add action to buttons on Alert, but it doesn't work. I just want to test if the alert button choice can change the text in label, but it doesn't work. Of course I can see the alert and buttons well, but nothing happens after clicking the button.

@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"
    }

}
4

5 回答 5

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"
}

}
于 2014-07-05T05:48:01.080 回答
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"
   }
}
于 2014-07-05T04:54:41.660 回答
3

您必须设置警报视图的委托:

alertTest.delegate = self

UIAlertView使用委托模式,这意味着它调用其委托上的方法来实现某些事情或通知事件。使用UIAlertView,其中一个事件是用户点击按钮时。为了让警报视图知道告诉谁用户的点击,您必须指定一个实现UIAlertViewDelegate协议的委托。您的代码实现了协议,但它从不告诉警报您想成为它的委托人,因此您永远不会收到方法调用。

于 2014-07-04T23:46:38.217 回答
2
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))
于 2015-10-13T14:36:55.053 回答
1

如果这适用于 iOS 8,您应该切换到 UIAlertController 和 UIAlertAction。UIAlertAction 包含按钮应该做什么。

于 2014-07-04T23:52:45.480 回答