58

Action Sheet iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/BRnqY3r
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

uQZRjaz.png!web

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

vueEvyb.png!web

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.

zIf6fuN.png!web

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

Z7J36jA.png!web

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 Runthe project, click the button and select the different actions inside the Action Sheet.

FzmyUzu.png!web

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK