iOS development: The correct way to implement a prompt box

Source: Internet
Author: User
Tags deprecated

In the upgrade process from IOS8 to iOS9, the pop-up cue box has changed a lot, in Xcode7, iOS9.0 SDK, has been explicitly prompted to no longer recommend the use of Uialertview, but only the use of Uialertcontroller, we demonstrate through the code.

I click on a button, and then pop-up the prompt box, the code example is as follows:

[OBJC] View plaincopyprint?

#import "ViewController.h"

@interface Viewcontroller ()

@property (strong,nonatomic) UIButton *button;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.button = [[UIButton alloc] Initwithframe:cgrectmake (0, [[UIScreen Mainscreen] bounds].size.width, 20)];

[Self.button settitle:@ "Jump" forstate:uicontrolstatenormal];

[Self.button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

[Self.view AddSubview:self.button];

[Self.button addtarget:self Action: @selector (ClickMe:) forcontrolevents:uicontroleventtouchupinside];

}

-(void) ClickMe: (ID) sender{

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "button was clicked" Delegate:self cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil Nil];

[Alert show];

}

@end

When you write the above code, you have the following warning tips:

"' Uialertview ' is deprecated:first deprecated into IOS 9.0-uialertview is deprecated. Use Uialertcontroller with a preferredstyle of Uialertcontrollerstylealert instead ".

Description Uialertview is first deprecated (not recommended) in IOS9. Let's go and use Uialertcontroller. But run the program, found that the code can still run successfully, does not appear crash.

But in the actual engineering development, we have such a "latent rule": to each warning (warning) as a mistake (error). So in order to adapt to Apple's trend, we solve this warning, use Uialertcontroller to solve this problem. The code is as follows:

[OBJC] View plaincopyprint?

#import "ViewController.h"

@interface Viewcontroller ()

@property (strong,nonatomic) UIButton *button;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.button = [[UIButton alloc] Initwithframe:cgrectmake (0, [[UIScreen Mainscreen] bounds].size.width, 20)];

[Self.button settitle:@ "Jump" forstate:uicontrolstatenormal];

[Self.button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

[Self.view AddSubview:self.button];

[Self.button addtarget:self Action: @selector (ClickMe:) forcontrolevents:uicontroleventtouchupinside];

}

-(void) ClickMe: (ID) sender{

Initialize the prompt box;

Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "hint" message:@ "button was clicked" PreferredStyle: Uialertcontrollerstylealert];

[Alert addaction:[uialertaction actionwithtitle:@ "OK" style:uialertactionstyledefault handler:^ (UIAlertAction * _ Nonnull action) {

Click on the button response event;

}]];

Pop-up prompt box;

[Self Presentviewcontroller:alert animated:true completion:nil];

}

@end

This way, the code will not have a warning.

The effect of the program after the same. Where preferredstyle this parameter has another choice: Uialertcontrollerstyleactionsheet. When this enumeration type is selected, the effect is as follows:

Discover that this cue box is ejected from the bottom. Is it simple? By looking at the code, you can also find that the button response in the prompt box no longer requires a delegate delegate to implement. Direct use of addaction can be in a block to implement button clicks, very convenient.

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.