Action Sheet Tutorial in iOS8 with Swift

In iOS8 the UIActionSheet object is deprecated, the preferred way to display an Action Sheet is by presenting an UIAlertController. In this tutorial we will display an Action Sheet with different action handlers. This tutorial is built in iOS 8 and Xcode 6.

Open Xcode and create a new Single View Application. For product name, use iOS8SwifActionSheetTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Select Swift as Language and make sure Universal is selected in Devices.

Go to the Storyboard, drag a Button from the Object Library to the View Controller inside the Storyboard. Double-click the Button and give it a title of "Display Action Sheet". Select the Button and ctrl - drag to the top of the view Controller, hold down ctrl and select "Top Space to Top Layout Guide" and "Center Horizontally in Container"

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the Button to the ViewController class and create the following Action

implement the showActionSheet method

@IBAction func showActionSheet(sender: AnyObject) {
    // 1
    let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet)
    // 2
    let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler: {
      (alert: UIAlertAction!) -> Void in
      println("File Deleted")
    let saveAction = UIAlertAction(title: "Save", style: .Default, handler: {
      (alert: UIAlertAction!) -> Void in
      println("File Saved")
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
      (alert: UIAlertAction!) -> Void in
    // 4
    // 5
    self.presentViewController(optionMenu, animated: true, completion: nil)
  1. an UIAlertController with the ActionSheet style is created
  2. two actions are created which can be added to the Alert Controller. Note the use of a closure inside the brackets of the handler parameter
  3. another action is created, this time with the Cancel style
  4. the actions are added to the Alert Controller
  5. The Alert Controller is presented.

Build and Run the project, click the button and select the different actions inside the Action Sheet.

You can download the source code of the iOS8SwiftActionSheetTutorial at the ioscreator repository on Github.