Action Sheet iOS Tutorial

An action sheet is a specific style of alert that appears in response to a control or action, and presents a set of two or more choices related to the current context.This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOS12ActionSheetTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

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". The Storyboard should look like this.

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

implement the displayActionSheet method

 @IBAction func displayActionSheet(_ sender: Any) {
     
    // 1
    let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .actionSheet)
        
    // 2
    let deleteAction = UIAlertAction(title: "Delete", style: .default)
    let saveAction = UIAlertAction(title: "Save", style: .default)
        
    // 3
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        
    // 4
    optionMenu.addAction(deleteAction)
    optionMenu.addAction(saveAction)
    optionMenu.addAction(cancelAction)
        
    // 5
    self.present(optionMenu, animated: true, completion: nil)
}
  1. a UIAlertController with the ActionSheet style is created
  2. two actions are created which can be added to the Alert Controller.
  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 IOS12ActionSheetTutorial at the ioscreator repository on Github.