iOS 8 UIAlertController 和 UIAlertAction

将alertView 和 actionSheet 封装在UIAlertController 里面化整为零,使开发者更便利

当我们一味的追求高内聚,低耦合的时候,伟大的苹果反其道而行之,这也告诉了我们一个道理:

只有水平高了,内聚也就高了,耦合度自然就低了!哈哈,废话少说,直接上图:

这是整个 demo 的效果图:

下面看看 alert 相关东西的实现,直接上代码:

- (IBAction)alertAction:(id)sender {

    UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"通知" message:@"请填写登陆信息!"  // 初始化   alert  preferredStyle:UIAlertControllerStyleAlert];

    

    [alter addTextFieldWithConfigurationHandler:^(UITextField *  textField) {                              

  /*注意:

  为 alert 添加textfield 注意 只有preferredStyle 

    为UIAlertControllerStyleAlert时候才能添加textfield

  在回调的block内配置  textfield 

  */ 

        [textField setFrame:CGRectMake(0, 50, 44, 200)];

        [textField setTextColor:[UIColor blueColor]];

        [textField setPlaceholder:@"NAMEFIELD"];

        [textField setClearButtonMode:(UITextFieldViewModeWhileEditing)];

        [textField setBorderStyle:UITextBorderStyleRoundedRect];

    }];

    [alter addTextFieldWithConfigurationHandler:^(UITextField *  textField) {

        [textField setFrame:CGRectMake(0, 100, 60, 200)];

        [textField setTextColor:[UIColor blueColor]];

        [textField setPlaceholder:@"PASSWORD"];

        [textField setSecureTextEntry:YES];

        [textField setClearButtonMode:(UITextFieldViewModeWhileEditing)];

        [textField setBorderStyle:UITextBorderStyleRoundedRect];

        

    }];

    

    [alter addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {

/*

  在这里添加动作的实现

*/

        NSLog(@"%@---%@",alter.textFields[0].text,alter.textFields[1].text);

    }]];

    [self presentViewController:alter animated:YES completion:nil];     // 显示 alert

}

代码撸好了,运行,点击alertAction  啪的一声,出现了如下的效果图

下面看看actionSheet 的实现  

- (IBAction)actionSheet:(id)sender {

    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *  action) {

        NSLog(@"---拍照获取!");

    // 再次添加实现

    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {

        NSLog(@"---从相册获取!");

      //在此添加实现

        

    }]];

    

    [self presentViewController:alert animated:YES completion:nil];

}

 然后运行点击  ,啪的一声:

更过的进阶技术可以关注公众号:进阶的脚步  回复学习资料  有惊喜哦

原文地址:https://www.cnblogs.com/feiyafeiblog/p/5043012.html