iPhone Programming: iOS 5 UIAlertView Styles

Опубликовано: 16 Июль 2026
на канале: MilmersXcode
4,843
21

In this tutorial I teach you how to change the style of a UIAlertView. It also includes how to get the text from the textFields.

Main Code:

(IBAction)normalAlert {

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Other", nil];
[alert setAlertViewStyle:UIAlertViewStyleDefault];
[alert setTag:0];
[alert show];
[alert release];

}


(IBAction)passwordAlert {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Other", nil];
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[alert setTag:1];
[alert show];
[alert release];

}

(IBAction)normalTextAlert {

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Other", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert setTag:2];
[alert show];
[alert release];

}

(IBAction)secureTextAlert {

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Other", nil];
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
[alert setTag:3];
[alert show];
[alert release];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 0) {
Password.text = @"";
Login_TextLabel.text = @"Normal Alert";
} else if (alertView.tag == 1) {
Password.text = [[alertView textFieldAtIndex:1] text];
Login_TextLabel.text = [[alertView textFieldAtIndex:0] text];
} else if (alertView.tag == 2) {
Password.text = @"";
Login_TextLabel.text = [[alertView textFieldAtIndex:0] text];
} else {
Password.text = @"";
Login_TextLabel.text = [[alertView textFieldAtIndex:0] text];
}
}

Don't forget to add the UIAlertViewDelegate to the .h!

Twitter:   / failcakeapps  
Apple Developer Center: http://developer.apple.com/devcenter/...
Website: http://failcake.webs.com/
Channel:    / milmersxcode