IOS Using Uialertview to show alerts

Source: Internet
Author: User
Tags uikit

Uialertview in other words, it ' s a dialog box. You want to show a message or the Ask user to confirm an action. Uialertview would come in handy. Here, I create a simple project and show a alert view when Suer click a button named "Show a simple Alert view". The implementation is in class Viewcontroller. Below is the main code.

viewcontroller.m//chapter1uialertview////Created by Winter on 15/8/13.//Copyright (c) 2015 YLD. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void    ) Viewdidload {[Super viewdidload];        Self.view.backgroundColor = [Uicolor Whitecolor];    UIButton *buttonsimple = [[UIButton alloc] Initwithframe:cgrectmake (20.0f, 30.0f, 300.0f, 30.0f)];    [Buttonsimple Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];    [Buttonsimple Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatehighlighted];    [Buttonsimple Setbackgroundcolor:[uicolor Orangecolor];    [Buttonsimple settitle:@ "Show A simple Alert View" forstate:uicontrolstatenormal];        [Buttonsimple addtarget:self Action: @selector (Showalert) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:buttonsimple];}             -(void) Showalert {Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Warning"                                           message:@ "Hello" delegate : Self cancelbuttontitle:@ "OK" ot        Herbuttontitles:nil, nil]; [Alertview show];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Run app and click the button you would see below scene.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Now, you probably appreciate to has the more buttons in Alert view and each button would invoke different function while user Clicking it. To implement this function, you need to make, view Controller accord with protocol uialertviewdelegate and implement met Hod alertview:clickedbuttonatindex:. I put a label at the top of the "view to see" button had clicked in Alert view and its button index. The Cancel button ' s index is 0.

The ViewController.h code as below.

  viewcontroller.h//  chapter1uialertview////  Created by Winter on 15/8/13.//  Copyright (c) 2015 YLD. All rights reserved.//#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller< uialertviewdelegate>{    UILabel *label_;} @end
ADD below codes to viewdidload in VIEWCONTROLLER.M file. These codes would create a label in top of the view and create a button next to last view element. When you click the This button it would invoke method Showalertwithmorebuttons.

    Label_ = [[UILabel alloc] Initwithframe:cgrectmake (0.0f, 30.0f, Self.view.bounds.size.width, 60.0f)];    Label_.backgroundcolor = [Uicolor blackcolor];    Label_.textalignment = Nstextalignmentcenter;    Label_.textcolor = [Uicolor whitecolor];    [Self.view Addsubview:label_];        UIButton *buttonmore = [[UIButton alloc] Initwithframe:cgrectmake (20.0f, 140.0f, 300.0f, 30.0f)];    [Buttonmore Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];    [Buttonmore Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatehighlighted];    [Buttonmore Setbackgroundcolor:[uicolor Browncolor];    [Buttonmore settitle:@ "Show more Buttons Alert View" forstate:uicontrolstatenormal];    [Buttonmore addtarget:self Action: @selector (showalertwithmorebuttons) forControlEvents: UIControlEventTouchUpInside];    [Self.view Addsubview:buttonmore];

Add method Alertview:Clickedbuttonatindex:and implement it.

-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) buttonindex {    Label_.text =[NSString stringwithformat:@ "%@%ld", [Alertview Buttontitleatindex:buttonindex], (long) buttonindex];}
Run the app and click button "Show more Buttons Alert View".



If you want the user input some text, you can set Alert view style as Uialertviewstyleplaintextinput
-(void) showalerttextinput {    Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Warning"                                                        message:@ "Do you like Apple?"                                                       Delegate:self                                              cancelbuttontitle:@ "Cancel"                                              otherbuttontitles:@ "OK", nil];        [Alertview Setalertviewstyle:uialertviewstyleplaintextinput];        [Alertview show];}

You can get the what user had input via[AlertviewTextfieldatindex: 0].text. I made it display in the label after user finishing inputting and clicking OK.

<pre name= "code" class= "OBJC" >-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (NSInteger) Buttonindex {    label_.text =[nsstring stringwithformat:@ "%@%ld textfiled 0:%@", [Alertview Buttontitleatindex: Buttonindex], (long) Buttonindex, [Alertview textfieldatindex:0].text];    }


If you want the user input some secure text, you can set Alert view style as Uialertviewstylesecuretextinput


If you want the user input login information user name and password, you can set Alert view style as UIALERTVIEWSTYLELOGINANDP Asswordinput. Use [Alertview textfieldatindex: 0]. text to get login text, [Alertview textfieldatindex: 1]. text to get password text.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

IOS Using Uialertview to show alerts

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.