Skip to content

Instantly share code, notes, and snippets.

@kennylugo
Created February 21, 2016 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennylugo/89e59b56a07d436056c3 to your computer and use it in GitHub Desktop.
Save kennylugo/89e59b56a07d436056c3 to your computer and use it in GitHub Desktop.
Closures with UIAlertController: Handlers
//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