iPhone – UIActionSheet Example

UIActionSheet is a cool way to get user input. The following example shows you how to implement the UIActionSheet.

1. Extend the UIActionSheetDelegate in the .h header file of the ViewController and add the (IBAction)showActionSheet:(id)sender method.

@interface MyViewController : UIViewController <UIActionSheetDelegate> {
	...
}

...

-(IBAction)showActionSheet:(id)sender;

@end

 

2. Add the following code in the .m implementation file. There are 5 parameters for initializing the UIActionSheet

  • initWithTitle:@”Title”
  • delegate:self
  • cancelButtonTitle:@”Cancel Button”
  • destructiveButtonTitle:@”Destructive Button”
  • otherButtonTitles:@”Other Button 1″, @”Other Button 2″, nil
-(IBAction)showActionSheet:(id)sender {
	UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", @"Other Button 2", nil];
	popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
	[popupQuery showInView:self.view];
	[popupQuery release];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 0) {
		self.label.text = @"Destructive Button Clicked";
	} else if (buttonIndex == 1) {
		self.label.text = @"Other Button 1 Clicked";
	} else if (buttonIndex == 2) {
		self.label.text = @"Other Button 2 Clicked";
	} else if (buttonIndex == 3) {
		self.label.text = @"Cancel Button Clicked";
	}

	/**
	 * OR use the following switch statement
	 * Suggested by Colin =)
	 */
	/*
	switch (buttonIndex) {
		case 0:
			self.label.text = @"Destructive Button Clicked";
			break;
		case 1:
			self.label.text = @"Other Button 1 Clicked";
			break;
		case 2:
			self.label.text = @"Other Button 2 Clicked";
			break;
		case 3:
			self.label.text = @"Cancel Button Clicked";
			break;
	}
	*/
}

 

3. Link the (IBAction)showActionSheet:(id)sender with a Hello Button in the main view such that the UIActionSheet will appear when user presses the Hello Button.

4. Text will be printed on the main view if u press the button in the UIActionSheet

 

Done =)

Reference: UIActionSheet, Part One: Alternate User Input

53 thoughts on “iPhone – UIActionSheet Example”

  1. thanks for this! I am wondering if you need to put this in a View Controller or if you can implement this in the app delegate. I am trying to choose a way to notify the user that the network is unavailable (or has become available) and figure that the AppDelegate is the place to a) listen for the notification and b) post the new status from. Any thoughts on this?

    Like

    1. i haven’t tried to make it in the app delegate. but as u can see in the above example, the view controller needs to implement the UIActionSheetDelegate. so i guess it should not work in the app delegate.

      i suggest u can put the checking logic in the app delegate as a global method and call it whenever it needs in the view controller.

      i am still a beginner in iPhone App. so please let me know if you can find a better way to do it =)

      Like

      1. hi, im still new with iphone development, can i have your full source code of this basic app ? cause i need to understand every single function, pleasee..

        thank you and i appreciate alot …

        regards,

        Isaputera

        Like

    1. you can show the UIActionSheet instance by the showInView method.
      (void)showInView:(UIView *)view

      Probably you need to add the codes in (void)applicationDidFinishLaunching:(UIApplication *)application if you want to pop up the actionsheet just after the application finishes launching.

      Like

  2. The only thing I don’t get is how to make the self.label reference work… it doesn’t build cleanly after entering the listed code. I’m pretty new to iOS and I’m sure it’s fairly easy to do, but I have no idea! Thanks for the cool post.

    Like

      1. I tried adding a label in the nib file and calling it ‘label’, but nothing was autogenerated in the viewcontroller.h so I’m not sure how the viewcontroller can ever refer to it as a self property.

        Like

      2. u need to define a UILabel in the controller header file and link the UILabel in the .nib to the controller. if i remember correctly, try the right click on the controller in the .nib and u could link them.

        Like

      3. Aha! control-linking from the File Owner to the Label solved the problem. Many thanks dude :-)

        Like

  3. Hey! I was wondering if you could open a new window using one of these UIActionSheet buttons. For example, if i want Other Button 1 to launch a window called WindowView, how would i go about it? Thanks for the cool post btw

    Like

      1. I already have a tabbed window with three tabs. However, each of the three tabs has a show details UIActionSheet button. I would like that button to navigate to the respective detailed window. For example, I would like to navigate to DetailedOne.xib, DetailedTwo.xib and DetailedThree.xib from each tab (One, Two, Three) respectively. How can i do that? Thanks :)

        Like

      2. Are u using the UITabBarController? if yes, you could take a look on the Apple Class Reference.

        If you just want to show the .xib, try

        window.view = theXibController.view
        

        I didn’t try the TabBarController before, so i am not sure if this is correct or not. hope it could help.

        Like

    1. Can you elaborate more about your problem?

      you can also modify the function such that

      -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        self.label.text = [NSString stringWithFormat:@"%d",buttonIndex];
      }
      

      and see what buttonIndex you get when you clicked the cancel button.

      Like

  4. Thanks for the great post I’ve edited the code a little so instead of the if statements it uses a switch. Makes it a bit cleaner to read

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
        switch (buttonIndex) {
            case 0:
               self.label.text = @"Destructive Button Clicked";
                break;
            case 1:
                 self.label.text = @"Other Button 1 Clicked";
                break;
            case 2:
                self.label.text = @"Other Button 2 Clicked";
                break;
            case 3:
                 self.label.text = @"Cancel Button Clicked";
                break;
        }
    }
    

    Like

      1. Hi! Im super newbie in xcode. Regarding the action sheet, I want all 3 buttons in that action sheet to jump into another view controller or any other controller.

        How will I call those controllers using the buttons in the action sheet?? Many thanks!

        Like

  5. Hi,

    I am trying to detect whether the user pressed the cancelButton or destructive button on the actionSheet. Depending on his/her input, I would like to modify the alert message shown on the screen.

    So I tried using if to check the buttonIndex, but it turns out that the buttonIndex value does not change with the input from the user.

    UIActionSheet *informUser = [[UIActionSheet alloc] initWithTitle:@"Missing data" delegate:self cancelButtonTitle:@"Thanks for telling me!" destructiveButtonTitle:@"I have ignored it!" otherButtonTitles:nil];
    
        [informUser showInView:self.view];
    
    //        NSLog(@"Cancel button = %d", informUser.cancelButtonIndex);
    //        NSLog(@"Destructive button = %d", informUser.destructiveButtonIndex);
    
        if(informUser.cancelButtonIndex == 1)
        {
            NSString *msg = [[NSString alloc] initWithFormat:@"Pls enter the number."];
    
            UIAlertView *alertUser = [[UIAlertView alloc] initWithTitle:@"Missing data" message:msg delegate:self cancelButtonTitle:@"Thanks!" otherButtonTitles:nil];
    
            [alertUser show];
        }
    

    I also tried using a separate method:

    -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
    

    but, I get wrong results when I have multiple UIAlert at different points on the same screen.

    In short, I would like to use a different UIAlert for different actions/inputs from user. Therefore, how do I detect which button on the actioSheet has been pressed by the user?

    Thank you.

    Like

    1. Did you implement the following function?

      -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
        switch (buttonIndex) {
          case 0:
            self.label.text = @"Destructive Button Clicked";
            break;
          case 1:
            self.label.text = @"Other Button 1 Clicked";
            break;
          case 2:
            self.label.text = @"Other Button 2 Clicked";
            break;
          case 3:
            self.label.text = @"Cancel Button Clicked";
            break;
        }
      }
      

      you could check the pressed input button by the buttonIndex.

      Like

  6. Actually Cancel button doesnt work .. so i edit d code as

     UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"" delegate:(id)self cancelButtonTitle:@"OK" destructiveButtonTitle:@"CANCEL" otherButtonTitles:@"Button1",@"Button2", nil];
     [sheet showFromToolbar:toolbar];
    

    instead of this code

    UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"" delegate:(id)self cancelButtonTitle:@"OK" destructiveButtonTitle:@"CANCEL" otherButtonTitles:@"Button1",@"Button2", nil];
    [popupQuery showInView:self.view];
    

    Like

  7. Excellent post! Found Exactly what I was looking for. Thanks for nice explanation. Cancel button does work (Reply in to last comment)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s