The correct implementation method of iOS9 using the prompt box

Source: Internet
Author: User

The correct implementation method of iOS9 using the prompt box

During the upgrade from iOS8 to iOS9, the pop-up prompt box has changed a lot. In the Xcode7 and iOS9.0 sdks, it has been explicitly prompted that UIAlertView is no longer recommended, you can only use UIAlertController. Let's use the code to demonstrate it.

Click a button and a prompt box is displayed. The sample code is as follows:

 

# Import ViewController. h @ interface ViewController () @ property (strong, nonatomic) UIButton * button; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. button = [[UIButton alloc] initWithFrame: CGRectMake (0,100, [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: @ The delegate: self cancelButtonTitle: @ OK otherButtonTitles: nil, nil]; [alert show];} @ end

When writing the above Code, the following warning prompt is displayed:

 

.

"'Uialertview' is deprecated: first deprecated in iOS 9.0-UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead ".

This indicates that UIAlertView is discarded (not recommended) in iOS9. Let's use UIAlertController. However, when you run the program, you will find that the code can still run successfully without crash.

.

However, in actual engineering development, we have such a "Potential Rule": Every warning should be treated as an error ). So in order to adapt to Apple's trend, we will solve this warning and use UIAlertController to solve this problem. The Code is as follows:

 

# Import ViewController. h @ interface ViewController () @ property (strong, nonatomic) UIButton * button; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. button = [[UIButton alloc] initWithFrame: CGRectMake (0,100, [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 {// initialization prompt box; UIAlertController * alert = [UIAlertController restart: @ prompt message: @ The preferredStyle: alert] button is clicked; [alert addAction: [UIAlertAction actionWithTitle: @ style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {// click the response event of the button;}]; // a prompt box is displayed. [self presentViewController: alert animated: true completion: nil];} @ end

In this way, the code will not have a warning.

 

The effect after running the program is the same as that after running the program. PreferredStyle has another option: UIAlertControllerStyleActionSheet. After this enumeration type is selected, the implementation result is as follows:

.

The prompt box is displayed from the bottom. Is it easy? By viewing the code, you can also find that the button response in the prompt box does not need to be implemented by the delegate. Using addAction directly, you can click buttons in a block, which is 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.