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

Analysis of Tencent Advertising’s direct e-commerce campaign in August!

With the standardization of direct-operated e-com...

After studying 10 big Douyin accounts, I found 8 Douyin promotion routines

Brother Xian has been feeling rather depressed la...

Why is there no user after the APP is launched?

What are the reasons why there are no users after...

iPhone XR becomes the most popular smartphone in 2019

The "Smartphone Model Market Tracking" ...

Why is there a ball on the roof? So many people don't know

Audit expert: Luo Huiqian Associate Researcher, I...

Tik Tok Operation Script Strategy

1. What is a short video script? A short video sc...

Guangdong embroidery: the four famous embroidery

Cantonese embroidery is composed of Guangzhou emb...

Google confesses to providing employee data to the FBI

On December 23, 2014, Christmas Eve, Google infor...

How does Bilibili operate and market? 8 tips!

Today I’m going to share with you some of Bilibil...