Use of UIActionSheet for iOS Learning

Source: Internet
Author: User

UIActionSheet is a selection button that appears in IOS. You can add multiple items and add click events for each item.

To quickly complete this example, open Xcode 4.3.2 and first create a single view application. Then add a button to the xib file to pop up the sheet view.

1. First, implement the Protocol in the. h file and add <UIActionSheetDelegate> at the end of the @ interface line to the Code. The protocol is equivalent to the interface in java and implements the methods in the Protocol.

@interface sheetviewViewController : UIViewController<UIActionSheetDelegate>@end

2. Add a button and name it showSheetView.

3. Create an Action ing for the button and map it to the. h file. Set the event type to Action and name it showSheet.

4. Add the Click Event code on the. m file.

The figure is as follows:

-(IBAction) showSheet :( id) sender {UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: @ "title," delegate: self cancelButtonTitle: @ "cancel" destructiveButtonTitle: @ "OK" otherButtonTitles: @ "first", @ "second", nil]; actionSheet. actionSheetStyle = UIActionSheetStyleBlackOpaque; [actionSheet showInView: self. view];}

ActionSheet. actionSheetStyle = UIActionSheetStyleBlackOpaque; // you can specify a style.

Parameter description:

CancelButtonTitle destructiveButtonTitle is automatically used by the system.

OtherButtonTitles is a defined item. Note that the last parameter is nil.

[ActionSheet showInView: self. view]; this line of statements indicates that the Action sheet is displayed in the current view. You can also use other methods to display the Action sheet.

Corresponding to the above diagram and code, you can see at a glance

5. How can we configure the event corresponding to the Action Sheet option? Methods In the implementation protocol. To see the effect of clicking Action sheet, we add UIAlertView to display the information. The following is an encapsulated method that passes in the corresponding information and displays the corresponding information in UIAlertView.

-(Void) showAlert :( NSString *) msg {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "Action Sheet option" message: msg delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert show];}

The Code executed by the Action Sheet option is as follows:

(Void) actionSheet :( UIActionSheet *) actionSheet clickedButtonAtIndex :( NSInteger) buttonIndex {if (buttonIndex = 0) {[self showAlert: @ "OK"];} else if (buttonIndex = 1) {[self showAlert: @ "first item"];} else if (buttonIndex = 2) {[self showAlert: @ "item 2"];} else if (buttonIndex = 3) {[self showAlert: @ "cancel"] ;}}-(void) actionSheetCancel :( UIActionSheet *) actionSheet {}-(void) actionSheet :( UIActionSheet *) actionSheet sheet :( NSInteger) buttonIndex {}-(void) actionSheet :( UIActionSheet *) actionSheet willDismissWithButtonIndex :( NSInteger) buttonIndex {}

You can see that buttonIndex is the index of the corresponding item.

Did you see the red button? It is a so-called destroy button supported by ActionSheet, which plays a warning role on an action of a user,

For example, when a message or image is permanently deleted. If you specify a destroy button, it will be highlighted in red:

ActionSheet. destructiveButtonIndex = 1;

Similar to the navigation bar, the operation form also supports three styles:

UIActionSheetStyleDefault // default style: white text displayed on the gray background

UIActionSheetStyleBlackTranslucent // transparent black background, white text

UIActionSheetStyleBlackOpaque // black background, white text

Usage:

ActionSheet. actionSheetStyle = UIActionSheetStyleBlackOpaque; // you can specify a style.

The first entry in the selected sheet is shown as follows:

6. Note: during the development process, it is found that the last click of UIActionSheet is invalid and valid only when the top half of the last item is clicked, this scenario is available only when UITabBar is used. Solution:

This is used in showView. [actionSheet showInView: [UIApplication sharedApplication]. keyWindow];

Or [sheet showInView: [AppDelegate sharedDelegate]. tabBarController. view]; in this way, no occlusion occurs.

Get code: http://download.csdn.net/detail/totogo2010/4343267

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.