How to use Uialertview1. The simplest usage
Uialertview*alert = [[Uialertview alloc]initwithtitle:@ "hint"
message:@ "This is a simple warning box! "
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alert show];
[Alert release];
2. Add multiple buttons for Uialertview
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 button that the user clicked
Uialertview has a delegate uialertviewdelegate that inherits the delegate to implement the Click event
Header file:
@interface myalertviewviewcontroller:uiviewcontroller<uialertviewdelegate> {
}
-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex;
-(ibaction) buttonpressed;
@end
Source file:
-(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 press the%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. Manually cancel the dialog box
[Alertdismisswithclickedbuttonindex:0 Animated:yes];
5: Add a child view for Uialertview
In the process of adding a sub-view to a Uialertview object too, it is a bit of attention that if you delete the button, that is, when you cancel all the buttons in the Uialerview view, the entire display structure may be unbalanced. The space that the button occupies does not disappear, and we can also understand that these buttons are not really deleted, just that he is not visible. If you are only using the Uialertview object to display text, you can add a newline character at the beginning of the message (@ \ n) to help balance the space at the bottom and top of the button.
The following code is used to demonstrate how to add a child view to a Uialertview object.
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. Uialertview All text is center-aligned by default. What if you need to align text to the left or add other controls such as an input box? Don't worry, the IPhone SDK is still flexible and has a lot of delegate messages for the calling program to use. All you have to do is
-(void) Willpresentalertview: (Uialertview *) Alertview
To modify or add as you want, for example, to align the message text to the left, the following code can be implemented:
-(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 simple, that is, when the message box is about to pop up, traverse all message box objects, and modify its text alignment property to Uitextalignmentleft.
Adding the other parts is also the same, adding two Uitextfield to the following code:
-(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 number";
Accoutpassword.placeholder = @ "Please enter password";
Accoutpassword.securetextentry = YES;
[Alertview Addsubview:accoutpassword];
[Alertview Addsubview:accoutname];
[Accoutname release];
[Accoutpassword release];
}
Displays the button and label shifts that are intrinsic to the message box, otherwise the text field you add will obscure it. Then add the required parts to the appropriate location.
For Uiactionsheet is actually the same, in
-(void) Willpresentactionsheet: (Uiactionsheet *) Actionsheet
Do the same thing in the same way you can get the interface you want.
How to use Uialertview