My path to learning Swift

Further Swift and IOS development

To learn a bit more, before improving my Noughts and Crosses game,  I undertook Ray Wenderlich’s IOS Apprentice Part I.

In the first exercise, to write a Bull’s Eye game, we came across:

  • UIAlertController, which is available in IOS 8.0 and later, and which displays an alert message to the user, with the provided (in order) title, message, and style. Possible styles are .Alert, and .ActionSheet. For example:
let alert = UIAlertController(title: "Hello, World", message: "This is my first app!", preferredStyle: .Alert)
  • UIAlertAction, which are the parameters to a button that you display to the user in the Alert Message. The parameters for UIAlertAction are the title of the button,  the style of the button, and a function to perform an action when the user touches the button. Possible styles are .Default.Cancel, and .Destructive. .Cancel will display a style which indicates to the user that the operation will be cancelled and things will remain unchanged, while .Destructive will display a style which indicates to the user that data will be changed or deleted. For example:
func someHandler(alert: UIAlertAction!) {
    // Do something...
}

let action = UIAlertAction(title: "Awesome!", style: .Default, handler: someHandler)

You tell the Alert Controller about the Alert Action by passing the action using the addAction method. For example, using the above two examples:

alert.addAction(action)
  • presentViewController, which commands IOS to display the Alert Message and the Alert Action(s) defined above. The parameters to pass to presentViewController are the configured UIAlertController, whether to animate the presentation (true or false), and a function (with no parameters and does not return a value) to execute upon completion of the presentation. For example, using the examples above:
presentViewController(alert, animated: true, completion: nil)

The order of events is:

  1. Create and define a UIAlertController
  2. Create and define a UIAlertAction
  3. Pass the action to the UIAlertController using it’s addAction method
  4. Show the Alert Message using presentViewController.

Note that presentViewController returns immediately, and programme execution continues with the statement following presentViewController. That is, Alert Messages are shown asynchronously.

One thought on “My path to learning Swift”

Leave a Reply

Your email address will not be published. Required fields are marked *