UIAlertController只能在iOS8以上使用,所以要慎用。
在iOS8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。(其实宝宝们心里都知道)
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建按钮
UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame =CGRectMake(50, 50, 100, 50);
[bt addTarget:self action:@selector(bt) forControlEvents:UIControlEventTouchDown];
[bt setBackgroundColor:[UIColor cyanColor]];
[self.view addSubview:bt];
[self.view setBackgroundColor:[UIColor yellowColor]];
}
//按钮的点击事件
-(void)bt
{
//UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。
//常规(default)、取消(cancel)以及警示(destruective
#pragma mark - 对话框
NSString *title = @"注意";
NSString *message = @"姓名和电话";
// NSString *cancelButtonTitle = @"Cancel";
NSString *otherButtonTitle = @"确定";
NSString *bbTitle=@"xx";
//
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// // UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *otherA = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *action=[UIAlertAction actionWithTitle:bbTitle style:UIAlertActionStyleDefault handler:nil];
// //[alertController addAction:cancel];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *aa){
NSLog(@"取消");
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"好的");
}];
// 带输入框的文本对话框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"账号";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
//将创建的按钮添加到控制器上
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:otherA];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
#pragma mark - 上拉菜单
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"标题" message:@"默认" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
NSLog(@"ok");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];