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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值