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.