"OK" button in the alert message -

0

Someone can help me

What is the right shape in Swift 2? asks to remove the "Let okAction", but then I lose the reference and the alert goes without button.

  

let okAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.Default, handler: nil)

Complete code to facilitate:

    @IBAction func singUpButtonTapped(sender: AnyObject) {

    let userEmail = userEmailAddressTextField.text
    let userPassword = userPasswordTextField.text
    let userPasswordRepeat = userPasswordRepeatTextField.text
    let userFirstName = userFirstNameTextField.text
    let userLastName = userLastNameTextField.text


    if( userPassword != userPasswordRepeat) {

        displayAlertMessage("Passwords do not match")
        return
    }

    if(userEmail!.isEmpty || userPassword!.isEmpty || userFirstName!.isEmpty || userLastName!.isEmpty )
    {
        displayAlertMessage("Passwords do not match")
        return
    }


}

func displayAlertMessage(userMessage:String)
{
    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

    self.presentViewController(myAlert, animated: true, completion: nil)

}
    
asked by anonymous 10.03.2016 / 19:47

1 answer

3

Your UIAlertAction is ok. As I have not actually posted the error presented, I believe it's warning of compilation warning that you are not using the created action, once unused it can be removed.

What you're missing is to add your action to alert :

func displayAlertMessage(userMessage:String)
{
    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

    myAlert.addAction(okAction)

    self.presentViewController(myAlert, animated: true, completion: nil)

}
    
10.03.2016 / 20:50