articles

Home / DeveloperSection / Articles / Creating app using UIActionSheet in iOS 6

Creating app using UIActionSheet in iOS 6

Tarun Kumar3495 26-Nov-2015

Here, we will learn about UIActionSheet class of Cocoa framework and also creating a sample using action sheet and its delegate methods. UIActionSheet class allow us to easily prompt the user for input by displaying a list of menu options. 

The action sheet having an optional title, cancel button title and one or more buttons. We can also present the action sheet from a tab bar, tool bar, button bar item or from a view. 

We can also use action sheet in iPad touch devices, when we tap outside the action sheet popover automatically dismiss the action sheet (by default). But we can dismiss it by programmatically.

 

For creating sample using UIActionSheet follow the steps:


1.    Open the Xcode and goto the File > New > click on ‘Project’. 
2.    Now, select ‘Application’ under the iOS pane and click on ‘Single View Application’ and click on ‘Next’ button. 
3.    Now, in the ‘Product Name’ field enter ‘ActionSheetSample’, enter the ‘Company Identifier’ like ‘com.mindstick’. select ‘iPhone’ form ‘Device Family’ options, uncheck the ‘Include Unit Tests’ and ‘Use Storyboards’ and check mark on ‘Use Automatic Reference Counting’, now click on ‘Next’ button and after click on ‘Create’ button. 
4.    Now, first of all we declare and define a method which will be responsible for bring up the action sheet, so click on the ‘ViewController.m’ file on the navigation controller and add this method in the interface declaration:


- (void)showActionSheet:(id)sender;

5. Now, in the viewDidLoad: method in the ViewController.m file, add the following code to create a button programmatically: 


UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10.0f, 10.0f, 300.0f, 55.0f);
[button setTitle:@"Show Action Sheet" forState:UIControlStateNormal];
[button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside]; 

Here, we are making the showActionSheet: custom method as a target for the button, when the button will be clicked it will call this method.   

6. Now, before calling any of the UIActionSheetDelegate methods, we will implement the UIActionSheetDelegate protocol in the ‘ViewController.h’ file, like this:


@interface ViewController : UIViewController<UIActionSheetDelegate>

 

7. Now, after implementing UIActionSheetDelegate we can implement its delegate methods in the ViewController.m file, so add this method:   


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
}

This method will automatically call when any button on the action sheet is tapped.  

8. Now, come on to the previous showActionSheet: method declaration in the implementation file, in this method we will create the action sheet which has five buttons, like this:


-(void)showActionSheet:(id)sender {
NSString *actionSheetTitle = @"Action Sheet Sample"; //Action Sheet Title
NSString *images = @"Images";
NSString *vedios = @"Vedios";
NSString *files = @"Files";
NSString *cancelTitle = @"Cancel Button";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:actionSheetTitle
delegate:self
cancelButtonTitle:cancelTitle
destructiveButtonTitle:nil
otherButtonTitles:images, vedios, files, nil];
....
}

at the time of action sheet declaration we are created six strings, for action sheet title, cancel button title, and three other button titles ( images, vedios, files ).

9. Now, come back to the delegate method actionSheet: clickedButtonAtIndex: of UIActionSheetDelegate, add these methods to identify the index no of action sheet, when the button is pressed. 


NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; 
if ([buttonTitle isEqualToString:@"Images"]) {
NSLog(@"Image Button is pressed");
}
if ([buttonTitle isEqualToString:@"Vedios"]) {
NSLog(@"Vedio Button is pressed");
}
if ([buttonTitle isEqualToString:@"Files"]) {
NSLog(@"File Button is pressed");
}
if ([buttonTitle isEqualToString:@"Cancel Button"]) {
NSLog(@"Canceling the ActionSheet");
}


Here we are using NSLog to print message in the console with each if statement, whenever any button is pressed of the action sheet. 

that’s it.

Now, go to ‘Product’ in the top menu bar and click on Run to build and run the project in the simulator.

And after click on ‘Show Action Sheet’ button to bring up the action sheet.

Here is the screen shot:


 

Creating app using UIActionSheet in iOS 6    Creating app using UIActionSheet in iOS 6

 

Source Code is here:  
ViewController.h file


#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UIActionSheetDelegate>

 

@end

 

ViewController.m file


#import "ViewController.h"

 

@interface ViewController ()

 

- (void)showActionSheet:(id)sender; //Declare method to show action sheet

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button.frame = CGRectMake(10.0f, 10.0f, 300.0f, 55.0f);

[button setTitle:@"Show Action Sheet" forState:UIControlStateNormal];

 [button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

}

 

//custom method

-(void)showActionSheet:(id)sender{

NSString *actionSheetTitle = @"Action Sheet Demo"; //Action Sheet Title

NSString *images = @"Images";

NSString *vedios = @"Vedios";

NSString *files = @"Files";

NSString *cancelTitle = @"Cancel Button";

UIActionSheet *actionSheet = [[UIActionSheet alloc]

initWithTitle:actionSheetTitle

delegate:self

cancelButtonTitle:cancelTitle

destructiveButtonTitle:nil

otherButtonTitles:images, vedios, files, nil];

[actionSheet showInView:self.view];

}

 

//ActionSheet delegate method

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

//Get the name of the current pressed button

NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];

if ([buttonTitle isEqualToString:@"Images"]) {

NSLog(@"Image Button is pressed");

}

if ([buttonTitle isEqualToString:@"Vedios"]) {

NSLog(@"Vedio Button is pressed");

}

if ([buttonTitle isEqualToString:@"Files"]) {

NSLog(@"File Button is pressed");

}

if ([buttonTitle isEqualToString:@"Cancel Button"]) {

NSLog(@"Canceling the ActionSheet");

}

}

 

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

@end

 Next, we will learn how to fetch device Images on click on Images button on our Action Sheet and displaying it on other view controller : Sample on displaying device images into UIViewController in iOS



Updated 07-Sep-2019

Leave Comment

Comments

Liked By