Created
February 21, 2016 17:39
-
-
Save kennylugo/89e59b56a07d436056c3 to your computer and use it in GitHub Desktop.
Closures with UIAlertController: Handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//When we select a cell, we want to bring up an alert | |
// We use the didSelectRowAtIndexPath method from UITableViewDelegate | |
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { | |
//CREATE MENU | |
let optionMenu = UIAlertController(title: "Select One", message: "Press one", preferredStyle: .Alert) | |
//MARK: Handler | |
let okButtonHandler = { (action:UIAlertAction!) -> Void in | |
let alertController = UIAlertController(title: "What you see", message: "You selected \(self.restaurantNames[indexPath.row])", preferredStyle: .Alert) | |
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil) | |
alertController.addAction(okAction) | |
self.presentViewController(alertController, animated: true, completion: nil) | |
} | |
//Add action to menu ( an action is the button in the modal ) | |
optionMenu.addAction(UIAlertAction(title: "OK", style: .Default, handler: okButtonHandler)) | |
//PRESENT THE MENU | |
self.presentViewController(optionMenu, animated: true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment