Friday, December 3, 2010


In this post we will have a look at UIActionSheet class.

I have divided this post into two half the first half deals with just showing the action sheet and the second half deals with performing some actions with the actions sheets like calling a method or performing some validations etc.

Design Phase: For this app I have took an object of the UIButton class and on the touch of the UIButton object I am calling a function which will display the UIActionSheet so heres how the final output will look like

Final Output

First Half of the post begins

Step 1: Open Xcode and select window based application give an appropriate name to your project and add two UIViewController subclass files to your project and save them with the name myviewController, now you can see two files with the same name but with different extension that's myviewController.h and myviewController.m

Step 2: Declare an object of UIButton and UIActionSheet in the .h file of myviewController along with a function which will help you to show the action sheet, the entire code looks like this

In the above code you can see a object method named displayActionSheet with the help of this method we will be calling the action sheet

Step 3: Now go to the .m file of myviewConteroller and select its init method where you will be performing some settings for the object of UIButton here how's it look

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
//Perform settings for button
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//setting the button frame
[btn setFrame:CGRectMake(67, 138, 160, 37)];
//setting the title and state of the button
[btn setTitle:@"Show Action Sheet" forState:UIControlStateNormal];
//calling a function on button touch
[btn addTarget:self action:@selector(displayActionSheet) forControlEvents:UIControlEventTouchUpInside];
    return self;

also now you will add the button view to the main view into the loadView function

Step 4: Now coming to our function the code is pretty much self explanatory and its just like a UIAlertView

-(void) displayActionSheet
myactionSheet = [[UIActionSheet alloc]initWithTitle:@"Action Sheet" delegate:nil cancelButtonTitle:@"Exit" destructiveButtonTitle:nil otherButtonTitles:@"Ok",nil];
//show the action sheet
[myactionSheet showInView:self.view];

Step 5: Go to the AppDelegate.m file of your project and add this view to the window heres the code to show you how

#import "ActionSheetAppDelegate.h"
#import "myviewController.h"

@implementation ActionSheetAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
myviewController *obj = [[myviewController alloc]init];
[window addSubview:obj.view];
    [window makeKeyAndVisible];

Press Build and Go and get the following output

Final Output

Second Half of this post begins

Well the first half was pretty easy right now we have to add some functionality to the buttons of the UIActionSheet

Step 1: Go to the displayActionSheet method and change the delegate from nil to self, yes you guessed it right we will be using a delegate method to give logic to the buttons of the UIActionSheet so go to the documentation and type UIActionSheet where you will get the UIActionSheet delegate methods from their select the

method, now with the help of simple if..else block we can identify which button of the action sheet was pressed and perform some coding as per the button press
Step 2: Copy and paste the method from the Documentation into the .m file of the myviewController and here's how will you give body to the delegate method

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
if(buttonIndex ==0)
//display alert view
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Action Sheet" message:@"Zero Index Pressed" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
[alert show];
else if(buttonIndex ==1)
//your application will exit

In the above code you are seeing the implementation of the delegate method of the UIActionSheet, here what i have done is if the button at zeroth index will be touched then display an instance of UIAlertView and if the button present at first index is pressed then exit the application.
Step 3: Friends before we move any further the delegate method of UIActionsheet is actually a protocol method so go to the .h file of myviewController and implement it like this

Step4: since you have already added the view to the window just hit build and go to get the following output

Ok button clicked

cancel button clicked

I hope this post was helpful Happy iCoding and have a great Day