Swift: Simple alert / bar button

Relax. You are already, even though you don’t yet know it, a much better Swift programmer than you have any reason to think you are. Once you’re comfortable with Cocoa Touch APIs, you’ve mastered nearly everything you need to know to keep developing iOS with Swift. Here’s an expansion of that Hello World view controller class, one that adds a simple bar button / alert.

class ViewController: UIViewController
    func respondToTap(bbi : UIBarButtonItem)
        let alert = UIAlertView()
        alert.title = "Hello!"

    override func loadView()
        self.view = UIView()
        self.view.backgroundColor = UIColor.whiteColor()
        self.title = "Hello World"

        let bbi = UIBarButtonItem(title: "Hello", style: UIBarButtonItemStyle.Plain, target: self, action: "respondToTap:")
        self.navigationItem.rightBarButtonItem = bbi

One Comment

  • And shouldn’t you write alert.addButton(title: “Okay!”) instead of alert.addButtonWithTitle(“Okay!”) ?