Saturday, February 14, 2015

Alerts with Swift for iOS

A few weeks ago I went over alerts with swift for Mac OS X. Well just to show how different alerts are between Mac OS X and iOS well take a look at several different type of alerts that can be used with iOS with swift.


First is the most basic alert that includes a title, message and up to 3 action buttons.



below is the code to set up the alert, and call it when a user taps on the button; btnShowAlert

//button action:
@IBAction func btnShowAlert(sender: AnyObject) {
      
       // set up the alert view
        let alertController = UIAlertController(title: "Hello World", message: "this is the obleo's alert message", preferredStyle: UIAlertControllerStyle.Alert)
       
       //set up the action button
        alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
       
        // show a alert view
        self.presentViewController(alertController, animated: true, completion: nil)
    }




That a little different to what we did with Mac OS X and swift. It has more code (however if you have auto complete on, most of its filled in for you.)

Another type alert that I use often is the alert with a text field. It has all the same options as above but includes a text field on the alert. ( you can have several fields if wanted, my example below screen shot show just one.)




To set up the alert with the field is not much different than the normal alert. There is just two added lines of code needed different than an normal alert. 1) add the field to the alert with addTextFieldWithConfigurationHandler and 2) setting up way to get the text added to the text field.

see code below:

//button action:
@IBAction func btnShowAskAlert(sender: AnyObject) {
        
// set up alert view
        let alertTextController = UIAlertController(title: "Your Name", message: "Name:", preferredStyle: UIAlertControllerStyle.Alert)
       
        // add the field to the alert (new)
        alertTextController.addTextFieldWithConfigurationHandler ({ (mainTextfld) -> Void in mainTextfld.text = "Add Your name"; })
       
        //add a button action
        alertTextController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
           
            // add code to preform action from user tapping OK button (new)
            let mainTextfld = alertTextController.textFields![0] as UITextField
            // present result on screen in UIText
            self.fldResult.text = mainTextfld.text
        }))
       
        // present the alert with field on screen
        self.presentViewController(alertTextController, animated: true, completion: nil)
       
        }

That it, swift is just amazing no mater if your targeting Mac or iOS. I noticing that swift for Mac is actually easier than iOS and is less lines of code than iOS for many of the same type of things.

Happy Coding ;)
 
PS I know its been a few weeks since I made a video, my old MBP finally died. I ran into issue with screenflow 3 is not working with my new Mac, I need to update screenflow to version 5+ and do not have the  extra cash today to make it happen. ( And yes I tried quicktime, but it not as professional as screenflow)

No comments:

Post a Comment