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.