0

Okay, so how am I able to access "textField" at the end of the block where it is added? I tried creating a global property called "alertTextField" then set it to the textField in the block so I can access it in the other action but it doesn't seem to be working. Any suggestions?

Thank you!

  @IBAction func resendPassword(sender: AnyObject) {

    let alertController : UIAlertController = UIAlertController(title: "Forgot Password?", message: "Enter your email", preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in

    }

    alertController.addAction(cancelAction)

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in

        textField == self.alertTextField

        textField.delegate = self
        textField.placeholder = "Email"
    }

    let sendAction: UIAlertAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.Default) { action -> Void in

        if (self.alertTextField?.text != nil) {

        PFUser.requestPasswordResetForEmailInBackground(self.alertTextField!.text)

        }

    }

    alertController.addAction(sendAction)

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

}
Echizzle
  • 3,359
  • 5
  • 17
  • 28

1 Answers1

0

Check out the sample code here for details: http://nshipster.com/uialertcontroller/

@IBAction func resendPassword(sender: AnyObject) {

    let alertController : UIAlertController = UIAlertController(title: "Forgot Password?", message: "Enter your email", preferredStyle: UIAlertControllerStyle.Alert)

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.placeholder = "Email"
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

    alertController.addAction(cancelAction)

    let sendAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.Default) { _ in
        // below is how you access the textField
        let emailTextField = alertController.textFields![0] as UITextField
        if let email = emailTextField.text {
            PFUser.requestPasswordResetForEmailInBackground(email)
        }
    }

    alertController.addAction(sendAction)

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

}
Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • @Echizzle how to accept an answer http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Leo Dabus Oct 20 '15 at 13:51