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

41 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?

    • 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 =)

    • 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.

  2. Pingback: iOS 개발 자료 총정리! | AstroRyan

  3. Pingback: Dailyon » Blog Archive » 아이폰 OS 개발 자료 총정리

  4. 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.

      • 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.

      • 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.

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

  5. Pingback: Message Box (Action Sheet) General « iOS Developers

  6. 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

      • 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 :)

      • 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.

  7. Pingback: เขียนโปรแกรม ipad //ลองใช้ xcode « hotfev.com

    • 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.

  8. 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;
        }
    }
    

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 )

Connecting to %s