Add a UITableView to an Existing ViewController in Xcode

Quick and dirty checklist for adding a table view to an existing view controller:

  1. Embed existing view controller in a NavigationController
  2. Drag a UITableView object into the view controller in the storyboard
  3. Make the UITableView the same width and height as the view controller and set the constraints to all four sides with no margins
  4. Create an outlet and connection for the tableView in the view controller class
  5. Add UITableViewDataSource and UITableViewDelegate to the class declaration. Your class should look something like this
    import UIKit
     
    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
     
        @IBOutlet weak var tableView: UITableView!
         
        override func viewDidLoad() {
            super.viewDidLoad()
        }
     
    }
  6. Select the table view, open the connections inspector and connect the table view’s dataSource and delegate methods to the View Controller object.
  7. If you’re not using static cells, open the attributes inspector and set the number of Prototype Cells to 1

Create a Confirmation Alert in iOS with Swift

If you want to present an alert with an option to either take an action or cancel:

@IBAction func clearImage(sender: UIBarButtonItem) {
    let alert = UIAlertController(title: "Clear Canvas", message: "Are you sure you want to clear the canvas?", preferredStyle: .Alert)
    let clearAction = UIAlertAction(title: "Clear", style: .Destructive) { (alert: UIAlertAction!) -> Void in
        self.canvas.image = nil
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default) { (alert: UIAlertAction!) -> Void in
        //print("You pressed Cancel")
    }
        
    alert.addAction(clearAction)
    alert.addAction(cancelAction)
        
    presentViewController(alert, animated: true, completion:nil)
}