How to add action to buttons in Alert in Swift
Asked Answered
L

5

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

}
Lombardo answered 4/7, 2014 at 23:42 Comment(0)
S
9

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

}
Suburb answered 5/7, 2014 at 5:48 Comment(0)
O
3

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.

Oliveolivegreen answered 4/7, 2014 at 23:46 Comment(0)
H
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"
   }
}
Harwill answered 5/7, 2014 at 4:54 Comment(0)
B
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)

}

If you don't want to do sth when the button is pressed:

alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
Brumbaugh answered 13/10, 2015 at 14:36 Comment(0)
G
1

If this is for iOS 8 you should switch to UIAlertController and UIAlertAction. The UIAlertAction contains what the button should do.

Greisen answered 4/7, 2014 at 23:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.