How to use IOS Uialertcontroller

Source: Internet
Author: User

In many languages, alert indicates a popup window, which is useful not only for formal app functions, but also for debugging purposes. In OC, the Uialertcontroller class is used to control the action of the popup window. Before iOS 8.0, it used the Uialertview class to control pop-ups, and if we wanted to popup a window with an input box, we could use the following code:

//Prior to IOS 8.0Uialertview *alert = [[Uialertview init] Initwithtitle:@"Alert Title"Message@"Alert Message" Delegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"OK", Nil];alert.alertviewstyle=Uialertviewstyleplaintextinput; [Alert Textfieldatindex:0].text =@"Show Text here";   [Alert show]; - (void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex {if(Buttonindex = =0)return; //Do something after clicking OK button}

We can see that if you want to implement the button function in the pop-up window, you need to use the proxy mode, the function code needs to be written in the Clickedbuttonatindex () function, which can be implemented, but it is not very convenient in some cases. For example, we have a uitableview, we click on any row to play the window action, then the above declaration of the pop-up window code needs to be written in the Didselectrowatindexpath () function, but because the popup button operation in another agent function, We cannot directly access the current number of rows Indexpath.row, can only be stored in a global variable, or we declare the popup window, the current number of rows bound to the tag of alert, so that in the agent function can be used directly.

After iOS 8.0, Uialertview was discarded and replaced by the Uialertcontroller class, and we wanted to use the following code to implement the same popup window:

//After IOS 8.0Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@"Alert Title"Message@"Alert Message"Preferredstyle:uialertcontrollerstylealert]; [Alert Addtextfieldwithconfigurationhandler:^ (Uitextfield *TextField) {Textfield.placeholder= Nslocalizedstring (@"Textplaceholder",@"Text");}]; Uialertaction*okbutton = [Uialertaction actionwithtitle:@"OK"Style:uialertactionstyledefault handler:^ (Uialertaction *action) {    //Do something after clicking OK buttonUitextfield *textfield =Alert.textFields.firstObject;}]; Uialertaction*cancelbutton = [Uialertaction actionwithtitle:@"Cancel"Style:uialertactionstylecancel handler:^ (Uialertaction *action) {   //Do something after clicking Cancel button}]; [Alert Addaction:okbutton]; [Alert Addaction:cancelbutton]; [Self Presentviewcontroller:alert animated:yes completion:nil];

We found that seems to be more complicated, although the code is a bit more, but completely abandon the proxy mode, and add buttons and input boxes to become more free, the button function code does not have to write in other agent functions, the whole structure at a glance, object-oriented feeling is blowing, feel very diao.

Resources:

Uialertcontroller Changes in IOS 8

How to use IOS Uialertcontroller

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.