Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 493
user3806731 Avatar asked Jul 04 '14 23:07

user3806731


People also ask

How do you display an action sheet?

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.

How do I close an alert in Swift?

You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object.


3 Answers

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

}
like image 60
Mayank Jain Avatar answered Oct 12 '22 14:10

Mayank Jain


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.

like image 25
drewag Avatar answered Oct 12 '22 14:10

drewag


@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"
   }
}
like image 36
Varsha Vijayvargiya Avatar answered Oct 12 '22 12:10

Varsha Vijayvargiya