user3806731
user3806731

Reputation: 919

How to add action to buttons in Alert in Swift

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

}

Upvotes: 7

Views: 17003

Answers (5)

Godlike
Godlike

Reputation: 1928

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))

Upvotes: 2

Varsha Vijayvargiya
Varsha Vijayvargiya

Reputation: 771

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

Upvotes: 3

drewag
drewag

Reputation: 94703

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.

Upvotes: 3

Mayank Jain
Mayank Jain

Reputation: 5754

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

}

Upvotes: 9

matt
matt

Reputation: 534925

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

Upvotes: 1

Related Questions