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

How to improve the conversion rate of new users?

We often say that while learning to maintain old ...

In the wave of madness and impetuousness, will Apple fall behind the 5G era?

Some time ago, China Mobile announced that the fi...

9 basic methods for brand marketing data analysis!

"What is your methodology for data analysis ...

Android Butterknife (Butter Knife) Usage Summary

Preface: ButterKnife is a View injection framewor...

The method of increasing users by “old customers bringing in new customers”!

Before talking about the method of “old employees...

Smart autumn harvest | Strawberries grown in the sky are sweet

Produced by: Science Popularization China Author:...

Apple has fixed the bug that caused iPhones to crash instantly

[[135484]] Earlier this week, iPhone users discov...