UIAlertController UIAlertAction

I have an app that worked with Swift 1, when I converted to Swift 2 the alert action is displayed briefly then it disappears before the user has time to enter anything into the 2 text fields and before the OK button is pressed.


var inputLatitude: UITextField?

var inputLongitude: UITextField?

let alertController = UIAlertController(title: "Location Manager Error", message: "Enter location manually", preferredStyle: .Alert)

alertController.addTextFieldWithConfigurationHandler({(textField: UITextField!) in

textField.placeholder = "Latitude"

inputLatitude = textField})

alertController.addTextFieldWithConfigurationHandler({(textField: UITextField!) in

textField.placeholder = "Longitude"

inputLongitude = textField})

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

if let userInfo = inputLatitude?.text {

self.stringLatitude = userInfo

}

if let userInfo2 = inputLongitude?.text {

self.stringLongitude = userInfo2

}

print(inputLatitude?.text)

print(inputLongitude?.text)

})

alertController.addAction(okAction)

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

foundLatitude.text = stringLatitude

foundLongitude.text = stringLongitude

the alert action is displayed briefly then it disappears before the user has time to enter anything into the 2 text fields and before the OK button is pressed.

I embedded your code in an @IBAction method, and I couldn't have find a behavior as such.

The problem may very probably hide somewhere in the lines you have not shown yet.


At least two lines of your code:

        foundLatitude.text = stringLatitude
        foundLongitude.text = stringLongitude

do not work as expected. (Or I may be mistaking your expectation for these two lines.)

They should be in the action handler of "OK".


Generally, you should not write any code after calling presentViewController:animated:completion: .

I moved the alert view out of the updateObservation function that it was in for updating Core Data. Now it is happy and works just fine. I put it into iys own funtion just below the foundLocation function as part of an error function. As I said now everything is happy. Thanks for the comments.

UIAlertController UIAlertAction
 
 
Q