tutorial on the use of Uialertview warning box components in IOS _ios

Source: Internet
Author: User

1. The simplest use
Initialization method:

Copy Code code as follows:

-(Instancetype) Initwithtitle: (NSString *) title message: (NSString *) message delegate: (ID/*<uialertviewdelegate >*/) Delegate Cancelbuttontitle: (NSString *) cancelbuttontitle otherbuttontitles: (NSString *) Otherbuttontitles, ...;

This method creates a warning box by setting the caption of a caption, content, proxy, and some buttons, and the code example reads as follows:
Uialertview * alert = [[Uialertview alloc]initwithtitle:@] my warning box "message:@" This is a warning box "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK, nil];
[Alert show];
The effect is as follows:

Note: If the number of buttons exceeds two, it will be created as follows:

If the number of buttons exceeds the screen display, a similar tableview effect is created.

2. Add multiple buttons for Uialertview

Copy Code code as follows:

Uialertview*alert = [[Uialertview alloc]initwithtitle:@ hint]

message:@ "Please select a button:"

Delegate:nil

cancelbuttontitle:@ "Cancel"

otherbuttontitles:@ "button One", @ "button two", @ "button three", nil];

[Alert show];

[Alert release];

3. How to determine the user clicks on the button
Uialertview has a delegate (proxy) Uialertviewdelegate that inherits the delegate to implement the Click event

Header file:

Copy Code code as follows:

@interface Myalertviewviewcontroller:uiviewcontroller {

}

(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex;
-(ibaction) buttonpressed;

@end


Source file:
Copy Code code as follows:

-(Ibaction) buttonpressed

{

Uialertview*alert = [[Uialertview alloc]initwithtitle:@ hint]

message:@ "Please select a button:"

Delegate:self

cancelbuttontitle:@ "Cancel"

otherbuttontitles:@ "button One", @ "button two", @ "button three", nil];

[Alert show];

[Alert release];

}

(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex
{

nsstring* msg = [[NSString alloc] initwithformat:@ "You pressed the first%d button!" ", Buttonindex];

uialertview* alert = [[Uialertview alloc]initwithtitle:@ hint]

Message:msg

Delegate:nil

cancelbuttontitle:@ "OK"

Otherbuttontitles:nil];

[Alert show];

[Alert release];

[MSG release];

}


Click "Cancel", "button One", "button two", "button three" index Buttonindex respectively is 0,1,2,3

4. Manual Cancel dialog box

Copy Code code as follows:

[Alertdismisswithclickedbuttonindex:0 Animated:yes];

5. Add a child view for Uialertview
in the process of adding a child view too much for a Uialertview object, it is a bit of a need to be aware that if you delete a button, that is, canceling all the buttons in the Uialerview view, it may cause the entire display structure to be unbalanced. The space that the button occupies does not disappear, we can also understand that these buttons are not really deleted, just that he is not visible. If you are only using text in a Uialertview object, you can add a newline character (@ \ n) at the beginning of the message to help balance the space on the bottom and top of the button.

The following code demonstrates how to add a child view to a Uialertview object.

Copy Code code as follows:

Uialertview*alert = [[Uialertview alloc]initwithtitle:@ ' Please wait '

Message:nil

Delegate:nil

Cancelbuttontitle:nil

Otherbuttontitles:nil];

[Alert show];

Uiactivityindicatorview*activeview = [[Uiactivityindicatorview alloc]initwithactivityindicatorstyle: Uiactivityindicatorviewstylewhitelarge];

Activeview.center = Cgpointmake (alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);

[ActiveView startanimating];

[Alert Addsubview:activeview];

[ActiveView release];

[Alert release];

6. Other
Uialertview All text is center-aligned by default. What if you need to align the text to the left or add other controls such as an input box? Don't worry, the IPhone SDK is still very flexible, with a lot of delegate messages for the caller to use. All you have to do is

Copy Code code as follows:

(void) Willpresentalertview: (Uialertview *) Alertview

In accordance with their own needs to modify or add, such as the need to align the message text left, the following code can be implemented:
Copy Code code as follows:

-(void) Willpresentalertview: (Uialertview *) Alertview

{

for (UIView * view in Alertview.subviews)

{

if ([View Iskindofclass:[uilabel class]])

{

uilabel* label = (uilabel*) view;

Label.textalignment=uitextalignmentleft;

}

}

}


This code is very simple, that is, when the message box is about to pop up, traverse all message box objects, their text alignment properties to Uitextalignmentleft can be modified.

Adding other parts is the same as adding two Uitextfield to the following code:

Copy Code code as follows:

-(void) Willpresentalertview: (Uialertview *) Alertview

{

CGRect frame = alertview.frame;

FRAME.ORIGIN.Y-= 120;

Frame.size.height + 80;

Alertview.frame = frame;

For (UIView * Viewin alertview.subviews)

{

if (![ Viewiskindofclass:[uilabelclass]])

{

CGRect btnframe = view.frame;

BTNFRAME.ORIGIN.Y + 70;

View.frame = Btnframe;

}

}

uitextfield* accoutname = [[Uitextfieldalloc] init];

uitextfield* Accoutpassword = [[Uitextfieldalloc] init];

Accoutname.frame = CGRectMake (Ten, FRAME.ORIGIN.Y + 40,frame.size.width-20, 30);

Accoutpassword.frame = CGRectMake (Ten, FRAME.ORIGIN.Y + 80,frame.size.width-20, 30);

Accoutname.placeholder = @ "Please enter account";

Accoutpassword.placeholder = @ "Please enter password";

Accoutpassword.securetextentry = YES;

[Alertview Addsubview:accoutpassword];

[Alertview Addsubview:accoutname];

[Accoutname release];

[Accoutpassword release];

}


Shows the shift of the button and label intrinsic to the message box, or the added text field will cover it. Then add the required parts to the appropriate location.

For the Uiactionsheet is actually the same, in

Copy Code code as follows:

(void) Willpresentactionsheet: (Uiactionsheet *) Actionsheet

To do the same processing can get the interface you want.

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.