Misunderstanding of UIActionSheet in ios

Source: Internet
Author: User


Today, I am studying UIActionSheet and directly put the code in viewDidLoad for execution. After a long time, I have always encountered problems, and I have been wondering if it is caused by xcode, later, we found that we usually put them in a button Method for operation, so there is a point that UIActionSheet must work with the action to achieve the effect. So I checked the development document, and the above sentence also verified the viewpoint: Action sheets display a set of buttons representing several alternative choices to complete a task initiated by the user.

Official documents: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UIActionSheet.html


Code:

@ Interface sheetviewViewController: UIViewController

@ End

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: @ "first ActionSheet" delegate: self cancelButtonTitle: @ "cancel" destructiveButtonTitle: @ "delete" Preserve ", nil];

ActionSheet. actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[ActionSheet showInView: self. view];


# Pragma mark --- implement the UIActionSheetDelegate Protocol
-(Void) actionSheet :( UIActionSheet *) actionSheet clickedButtonAtIndex :( NSInteger) buttonIndex {

If (buttonIndex = 0 ){
[Self showAlert: @ "OK"];
} Else if (buttonIndex = 1 ){
[Self showAlert: @ "first"];
} Else if (buttonIndex = 2 ){
[Self showAlert: @ "item 2"];
} Else if (buttonIndex = 3 ){
[Self showAlert: @ "cancel"];
}

NSString * buttonTitle = [actionSheet buttonTitleAtIndex: buttonIndex];
NSLog (@ "buttonTitle = % @", buttonTitle );
}

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.


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.





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.