Action Sheet iOS Tutorial
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.
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.
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) }
- a UIAlertController with the ActionSheet style is created
- two actions are created which can be added to the Alert Controller.
- another action is created, this time with the Cancel style
- the actions are added to the Alert Controller
- The Alert Controller is presented.
Build and Runthe project, click the button and select the different actions inside the Action Sheet.
You can download the source code of the IOS12ActionSheet Tutorial at the ioscreator repository on Github .
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK