The following code is for a function I wrote to handle the 'Back' button and the 'Logout' button. When the user taps 'Cancel', he will stay on the current view, when he taps 'OK' he will be taken to either the previous view or the login screen, depending on which button he tapped. Can I do this with one function or do I need a function for each button? I would prefer one function that I can make a global function and use across all three views.
func passThroughErrorAlertController(title:String, error:String) {
var passThroughAlert = UIAlertController(title: title, message: passThroughError, preferredStyle: UIAlertControllerStyle.Alert)
passThroughAlert.addAction((UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)))
passThroughAlert.addAction((UIAlertAction(title: "OK", style: .Default, handler: {action in
self.dismissViewControllerAnimated(true, completion: nil)
})))
self.presentViewController(passThroughAlert, animated: true, completion: nil)
}
Could I use performSegueWithIdentifier
and then use a var with the segue identifier? What would that look like? By which I mean, are there added arguments I need after performSegueWithIdentifier
?
You would simply specify a different identifier, depending on which view controller you are unwinding to.
Your AlertController would specify the correct identifier, depending on whether the user had logged out (unwind to login), or not (unwind to previous view).
If you need to pass additional information, you can set that up in prepareForSegue.