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"
}
}
To display your action sheet in a popover, specify your popover's anchor point using the popoverPresentationController property of your alert controller. It is safe to configure this property regardless of the underlying device.
You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object.
Is your clickedButtonAtIndex method calling? Put a breakpoint and debug the code. You doesn't set the delegate of 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"
}
}
You have to set the delegate of the alert view:
alertTest.delegate = self
UIAlertView
uses the delegate pattern which means it calls methods on its delegate to achieve certain things or notify of events. With UIAlertView
, one of those events is when the user taps a button. For the alert view to know who to tell about a user's tap, you must specify a delegate that implements the UIAlertViewDelegate
protocol. Your code implements the protocol but it never tells the alert that you want to be its delegate so you never receive the method call.
@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"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With