<span id="Label3"></p><p><p>In the upgrade from IOS8 to iOS9, the way to pop up the box has changed a lot, in Xcode7, IOS9.0 's SDK has made it clear that we are no longer recommending the use of uialertview, but only using uialertcontroller, which we demonstrate through the Code.</p></p><p><p>I click on a button and then pop up the prompt box, The code example is as Follows:</p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">#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,)]; [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]; [alert show]; } @end</pre></pre><br>The following warning prompts are available when you write the above code:<p><p></p></p><p><p>。</p></p><p><p>"' Uialertview ' is Deprecated:first deprecated on IOS 9.0-uialertview is Deprecated. Use Uialertcontroller with a preferredstyle of Uialertcontrollerstylealert instead ".</p></p><p><p>Description Uialertview is first deprecated (not recommended) in iOS9. Let's go with uialertcontroller. however, running the program, The discovery code can still run successfully, and there will be no Crash.</p></p><p><p>。</p></p><p><p>But in the actual engineering development, we have such a "latent rule": we should treat every warning (warning) as Error. So in order to conform to Apple's trend, We solve this warning, use Uialertcontroller to solve this problem. The code is as Follows:</p></p><p><p></p></p><pre name="code" class="objc">#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{//initialization prompt box; uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@ "hint" Me ssage:@ "button was clicked" preferredstyle:uialertcontrollerstylealert "; [alert addaction:[uialertaction actionwithtitle:@ "ok" style:uialertactionstyledefault handler:^ (UIAlertAction * _ Nonnull Action) {//click the Button's response event;}]]; Popup notification box; [self Presentviewcontroller:alert animated:true completion:nil];} @end </pre><br>This way, the code will not have a warning.<br><p><p></p></p><p><p>The effect of the program after the Same. Where preferredstyle this parameter has another option: Uialertcontrollerstyleactionsheet. After selecting this enumeration type, the implementation is as Follows:</p></p><p><p>。</p></p><p><p>Notice that this box is ejected from the Bottom. Isn't it simple? By looking at the code, you can also see that the button response in the prompt box no longer requires the delegate delegate to be Implemented. It is very convenient to use Addaction directly to implement button clicks in a Block.</p></p><p><p><br></p></p><p><p><br></p></p><p><p>GitHub home: https://github.com/chenyufeng1991. Welcome everyone to Visit!<br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>IOS9 the correct way to use the prompt box</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service