In this tutorial, we are going to discuss about implementation of another type of Alert controller i.e. ActionSheet using Objective-C Language in iOS application in Xcode 8. ActionSheet is used to display options to choose from.
Following are the steps to implement ActionSheet:
Step 1: Open Xcode -> click on Create new Xcode project then choose Single View Application template under iOS tab as shown in below image and click Next button.
Step 2: Enter the product name as ActionSheetDemo, select the Language as Objective-C and again click Next button as shown in below image. Later click Create button to create project appear on the next window.
Step 3: Adding UIButton to the View Controller.
On your project, click on main.storyboard file available in the project navigator, and then select the UIButton from object library, drag into the superview of View Controller, it will look like below.
Step 4: Declaring IBAction property for Button.
Select the Assistant Editor and create IBAction property for UIButton by holding Ctrl button. It will open up a popup view like below then enter the Name as ‘Action Sheet’ and click Connect.
The property IBAction is defined because ActionSheet will be displayed on action event of Button where its code is written.
Step 5: Complete Code in ViewController.m file
In ViewController.m file, code to present ActionSheet is written on action method of button as shown in below screenshot.
Most Importantly, we have two preferredStyle in AlertController as follows:
Hence, we need to choose ‘UIAlertControllerStyleActionSheet’ for showing ActionSheet.
For adding button into ActionSheet, we need to define buttons using class ‘UIAlertAction’ and then need to add as subView of ActionSheet.
Lastly, we need to present ActionSheet. That’s it.
Step 6: Finally, Build and Run the application where you will get the output like below.
Have Fun !
You can also watch its video on below link: