一、老的警告框
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title"
message:@"message" delegate:nil cancelButtonTitle:@"NO"
otherButtonTitles:@"YES",nil];
2.设置警告框的代理对象,当用户对alertView有任何操作时,都会交给代理对象来进行响应
alert.delegate = self;
3.设置警告框的显示样式(共4种)
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
4.显示警告框
[alert show];
5.成为委托方代理需遵循协议<</span>UIAlertViewDelegate>,协议中方法有:
- (void)alertView:(UIAlertView *)alertView
clickeonAtIndex:(NSInteger)buttonIndex; //点击按钮携带文本框信息
- (void)alertViewCancel:(UIAlertView *)alertView;//点击cancel按钮
- (void)willPresentAlertView:(UIAlertView *)alertView;//警告框即将展现
- (void)didPresentAlertView:(UIAlertView *)alertView;//警告框已经展现
- (void)alertView:(UIAlertView *)alertView
willDismissWithButtonIndex:(NSInteger)buttonIndex;//文本框即将消失
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex;//文本框已经消失
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView;
二新版警告框(支持ios8以上系统,目前iphone4最高能升到7.1系统)
1.创建alertController实例,并说明样式
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"title" message:@"message"
preferredStyle:UIAlertControllerStyleAlert];
2.创建按钮行为
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES"
style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
UITextField *nameText = alertController.textFields[0]; //获取在文本框中输入的文字
NSLog(@"%@",nameText.text);
}];
UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"NO"style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了NO按钮");
}];
3.添加行为到控制器中
[alertController addAction:yesAction];
[alertController addAction:noAction];
4.添加文本框到控制器中
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//对添加的文本框进行设置
textField.font = [UIFont systemFontOfSize:24];
textField.textColor = [UIColor redColor];
}];
5.推出控制器,显示界面
[self presentViewController:alertController animated:YES completion:nil]; //两个控制器之间的切换,方法一就是使用present方法
}
6.UIAlertControllerStyle类型:
1、 UIAlertControllerStyleActionSheet = 0
2、 UIAlertControllerStyleAlert