IOS development-ActionSheet usage and pop-up dialog box

Source: Internet
Author: User

IOS development-ActionSheet usage and pop-up dialog box

In our iOS development, we often see the following interface requirements:

.

 

That is, click the button and a selection prompt box appears. We use two methods today (ActionSheet and AlertController) to implement this function. Sample Code uploads to: https://github.com/chenyufeng1991/iOS-ActionSheet.

[Implemented using ActionSheet]

(1) The implementation code is as follows:

 

# Import "ViewController. h" @ interface ViewController ()
 
  
@ End @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];} # pragma mark-click the event-(IBAction) actionSheetButtonPressed :( id) sender {/** UIActionSheet has been discarded after 8.3. If you want to remove the warning information, you can set the Deployment Target of the project to below 8.3 to remove the warning. * // ** Title: if you do not want a title, you can set it to nil. Note that you need to implement UIActionSheetDelegate; destructiveButtonTitle: The set button text is red; otherButtonTitles: In the order of buttons; */UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: @ "this is the title" delegate: self cancelButtonTitle: @ "cancel" destructiveButtonTitle: @ "OK" Weight: @ "", @ "Entertainment", nil];/*** UIActionSheetStyleAutomatic UIActionSheetStyleDefault UIActionSheetStyleBlackTranslucent UIActionSheetStyleBlackOpaque * // The actionSheetStyle here can also be left unspecified; actionSheet. actionSheetStyle = UIActionSheetStyleAutomatic; [actionSheet showInView: self. view];}/*** method for automatic callback in UIActionSheetDelegate; process the RESPONSE event in it; */# pragma mark-UIActionSheetDelegate-(void) actionSheet :( UIActionSheet *) actionSheet clickedButtonAtIndex :( NSInteger) buttonIndex {// The Order of buttons is 0-N; switch (buttonIndex) {case 0: NSLog (@ "Click OK"); break; case 1: NSLog (@ ""); break; case 2: NSLog (@ ""); break; case 3: NSLog (@" "); break; default: break; }}@ end
 

(2) Since my project is deployed on iOS9 (Deployment Target is 9.0), the above Code will report a warning:

 

.

Indicates that the UIActionSheet has been discarded after iOS8.3. We recommend that you use UIAlertControllerStyleActionSheet in UIAlertController.

But handle this type of warning (*** is deprecated: first deprecated in iOS ...) there is an opportunistic way to directly set the Deployment Target of our project to an abandoned version. For example, if *** has been discarded in 9.0, we set Deployment Target to 8. x will not report a warning, set it to 9. x, a warning will be reported, as long as it is earlier than the version used to start discarding.

(3) run the program. The above implementation results are as follows:

.

 

 

[Implemented using AlertController]

Since Alibaba officially recommends that we use AlertController to replace ActionSheet, we also use AlertController for implementation. For more information about AlertController usage, see iOS9 prompt box correct implementation.

(1) The code is implemented as follows:

 

# Import "SecondViewController. h "@ interface SecondViewController () @ end @ implementation SecondViewController-(void) viewDidLoad {[super viewDidLoad] ;}# pragma mark-pop-up selection prompt box-(IBAction) buttonPressed :( id) sender {// initialization prompt box;/** preferredStyle parameter: Alert, UIAlertControllerStyleAlert * if you want to implement the ActionSheet effect, the preferredStyle here should be set to alert instead of UIAlertControllerStyleAlert; */UIAlertController * alert = [UIAlertController alertControllerWithTitle: @ "prompt" message: nil preferredStyle: blank];/*** style parameter: UIAlertActionStyleCancel, UIAlertActionStyleDestructive (the default button text is red) ** // place each button in sequence. [alert addAction: [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {// RESPONSE event for clicking the button; NSLog (@ "Click OK") ;}]]; [alert addAction: [UIAlertAction actionWithTitle: @ "Sports" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {// RESPONSE event when a button is clicked; NSLog (@ "");}]; [alert addAction: [UIAlertAction actionWithTitle: @ "Entertainment" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {// RESPONSE event of the click button; NSLog (@ "") ;}]]; [alert addAction: [UIAlertAction actionWithTitle: @ "" style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * _ Nonnull action) {// RESPONSE event of the click button; NSLog (@ "canceled") ;}]]; // a prompt box is displayed; [self presentViewController: alert animated: true completion: nil] ;}# pragma mark-click (IBAction) backPressed (id) sender {[self dismissViewControllerAnimated: true completion: nil] ;}@ end

(2) The running effect is as follows: it can basically achieve the same effect as ActionSheet.

 

.

[Comparison Between ActionSheet and AlertController]

Compare the above two implementation methods to see if they are different:

(1) When using ActionSheet, clicking other areas outside the prompt box is equivalent to clicking the "cancel" button and the prompt box disappears. When using AlertController, click the blank area except the prompt box, the interface has no response.

(2) When ActionSheet is used, there is a blank interval between the "cancel" button and other buttons. When AlertController is used, all buttons are connected.

To sum up, you can select different implementation methods based on your actual development needs. Of course, if you have learned how to customize controls, you can also customize controls.


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.