The correct way to use the prompt box in iOS9

The correct way to use the prompt box in iOS9

[[154064]]

In the process of upgrading from iOS8 to iOS9, the way of popping up prompt boxes has changed a lot. In Xcode7 and iOS9.0 SDK, it has been clearly pointed out that it is no longer recommended to use UIAlertView, and only UIAlertController can be used. Let's demonstrate it through code.

I click a button and a prompt box pops up. The code example is as follows:

  1. [objc] view plaincopyprint?
  2.  
  3. # import   "ViewController.h"  
  4.  
  5. @interface ViewController ()
  6.  
  7. @property (strong,nonatomic) UIButton *button;
  8.  
  9. @end  
  10.  
  11. @implementation ViewController
  12.  
  13. - ( void )viewDidLoad {
  14. [ super viewDidLoad];
  15.  
  16. self.button = [[UIButton alloc] initWithFrame:CGRectMake( 0 , 100 , [[UIScreen mainScreen] bounds].size.width, 20 )];
  17. [self.button setTitle:@ "Jump" forState:UIControlStateNormal];
  18. [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  19. [self.view addSubview:self.button];
  20.  
  21. [self.button addTarget:self action: @selector (clickMe:) forControlEvents:UIControlEventTouchUpInside];
  22.  
  23. }
  24.  
  25. -( void )clickMe:(id)sender{
  26.  
  27. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@ "Prompt" message:@ "The button was clicked" delegate:self cancelButtonTitle:@ "OK" otherButtonTitles:nil, nil nil];
  28. [alert show];
  29.  
  30. }
  31.  
  32. @end   


When writing the above code, the following warning will appear:

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

This means that UIAlertView was first deprecated (not recommended) in iOS 9. Let's use UIAlertController. However, when running the program, it is found that the code can still run successfully without crashing.

But in actual engineering development, we have such an "unspoken rule": to treat every warning as an error. So in order to follow Apple's trend, we solve this warning and use UIAlertController to solve this problem. The code is as follows:

  1. [objc] view plaincopyprint?
  2.  
  3. # import   "ViewController.h"  
  4.  
  5. @interface ViewController ()
  6.  
  7. @property (strong,nonatomic) UIButton *button;
  8.  
  9. @end  
  10.  
  11. @implementation ViewController
  12.  
  13. - ( void )viewDidLoad {
  14. [ super viewDidLoad];
  15.  
  16. self.button = [[UIButton alloc] initWithFrame:CGRectMake( 0 , 100 , [[UIScreen mainScreen] bounds].size.width, 20 )];
  17. [self.button setTitle:@ "Jump" forState:UIControlStateNormal];
  18. [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  19. [self.view addSubview:self.button];
  20.  
  21. [self.button addTarget:self action: @selector (clickMe:) forControlEvents:UIControlEventTouchUpInside];
  22.  
  23. }
  24.  
  25. -( void )clickMe:(id)sender{
  26.  
  27. //Initialize the prompt box;  
  28. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@ "Prompt" message:@ "The button was clicked" preferredStyle: UIAlertControllerStyleAlert];
  29.  
  30. [alert addAction:[UIAlertAction actionWithTitle:@ "OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  31. //Click the button to respond to the event;  
  32. }]];
  33.  
  34. //Pop up prompt box;  
  35. [self presentViewController:alert animated: true completion:nil];
  36.  
  37.  
  38. }
  39.  
  40.  
  41.  
  42. @end   


This way, there will be no warnings in the code.

The effect after the program is run is the same as above. There is another option for the preferredStyle parameter: UIAlertControllerStyleActionSheet. After selecting this enumeration type, the effect is as follows:

We can see that this prompt box pops up from the bottom. Isn't it simple? By looking at the code, we can also find that the button response in the prompt box no longer needs the delegate to be implemented. We can directly use addAction to implement the button click in a block, which is very convenient.

<<:  The most practical Android architecture design principles

>>:  How should a designer with zero foundation learn front-end

Recommend

Why do deep space probes circle before reaching their destination?

According to foreign media reports, the BepiColom...

How to write good copy? I have summarized 17 methods!

1. First you need to digest the information about...

Online server rental price for about 60,000 people

What is the price of renting an online server for...

Xiaohongshu KOL operation strategy from 1 to 100!

Introduction Users' following, comments, unfo...

User operation: 8 tricks for user portrait model!

User portraits are a way of describing and depict...

It only takes five steps to quickly build an API based on the terminal environment

【51CTO.com Quick Translation】Would you like to qu...

Albert: Learn English without taking detours

Albert: Learn English without taking detours Reso...