UIAlertController的使用——创建提示窗口

来源:互联网 发布:wifi信号搜索软件 编辑:程序博客网 时间:2024/06/06 01:26


UIAlertController只能在iOS8以上使用,所以要慎用。

在iOS8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。(其实宝宝们心里都知道大笑大笑





- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //创建按钮

    UIButton *bt=[UIButtonbuttonWithType:UIButtonTypeCustom];

    bt.frame =CGRectMake(50,50, 100, 50);

    [bt addTarget:selfaction:@selector(bt)forControlEvents:UIControlEventTouchDown];

    [bt setBackgroundColor:[UIColorcyanColor]];

    [self.viewaddSubview:bt];

    [self.viewsetBackgroundColor:[UIColoryellowColor]];


 

}

//按钮的点击事件

-(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 = [UIAlertControlleralertControllerWithTitle:title message:messagepreferredStyle:UIAlertControllerStyleAlert];

//    // UIAlertAction *cancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:nil];

    UIAlertAction *otherA = [UIAlertActionactionWithTitle:otherButtonTitle style:UIAlertActionStyleDefaulthandler:nil];

    UIAlertAction *action=[UIAlertActionactionWithTitle:bbTitle style:UIAlertActionStyleDefaulthandler:nil];

//    //[alertController addAction:cancel];

    

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *aa){

        NSLog(@"取消");

    }];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"好的"style:UIAlertActionStyleDefaulthandler:^(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];

  

  [selfpresentViewController:alertController animated:YEScompletion:nil];

  



#pragma mark - 上拉菜单

 

    UIAlertController *alertController=[UIAlertControlleralertControllerWithTitle:@"标题"message:@"默认"preferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction *cancelAction=[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    UIAlertAction *okAction=[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action){

        NSLog(@"ok");

    }];

    [alertController addAction:cancelAction];

    [alertController addAction:okAction];

    

    [selfpresentViewController:alertController animated:YEScompletion:nil];

  



0 0
原创粉丝点击