In the upgrade process from IOS8 to iOS9, the pop-up cue box has changed a lot, in Xcode7, iOS9.0 SDK, has been explicitly prompted to no longer recommend the use of Uialertview, but only the use of Uialertcontroller, we demonstrate through the code.
I click on a button, and then pop-up the prompt box, the code example is as follows:
[OBJC] View plaincopyprint?
#import "ViewController.h"
@interface Viewcontroller ()
@property (strong,nonatomic) UIButton *button;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Self.button = [[UIButton alloc] Initwithframe:cgrectmake (0, [[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:@ "button was clicked" Delegate:self cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil Nil];
[Alert show];
}
@end
When you write the above code, you have the following warning tips:
"' Uialertview ' is deprecated:first deprecated into IOS 9.0-uialertview is deprecated. Use Uialertcontroller with a preferredstyle of Uialertcontrollerstylealert instead ".
Description Uialertview is first deprecated (not recommended) in IOS9. Let's go and use Uialertcontroller. But run the program, found that the code can still run successfully, does not appear crash.
But in the actual engineering development, we have such a "latent rule": to each warning (warning) as a mistake (error). So in order to adapt to Apple's trend, we solve this warning, use Uialertcontroller to solve this problem. The code is as follows:
[OBJC] View plaincopyprint?
#import "ViewController.h"
@interface Viewcontroller ()
@property (strong,nonatomic) UIButton *button;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Self.button = [[UIButton alloc] Initwithframe:cgrectmake (0, [[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{
Initialize the prompt box;
Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "hint" message:@ "button was clicked" PreferredStyle: Uialertcontrollerstylealert];
[Alert addaction:[uialertaction actionwithtitle:@ "OK" style:uialertactionstyledefault handler:^ (UIAlertAction * _ Nonnull action) {
Click on the button response event;
}]];
Pop-up prompt box;
[Self Presentviewcontroller:alert animated:true completion:nil];
}
@end
This way, the code will not have a warning.
The effect of the program after the same. Where preferredstyle this parameter has another choice: Uialertcontrollerstyleactionsheet. When this enumeration type is selected, the effect is as follows:
Discover that this cue box is ejected from the bottom. Is it simple? By looking at the code, you can also find that the button response in the prompt box no longer requires a delegate delegate to implement. Direct use of addaction can be in a block to implement button clicks, very convenient.