美文网首页UIiOSiOS进阶指南
UIAlertController简单使用

UIAlertController简单使用

作者: ManoBoo | 来源:发表于2015-12-07 17:55 被阅读451次

在iOS8之前的开发过程中,我们通常使用UIAlertView或者UIActionSheet来提示用户是否进行某项操作,但是其使用都过于繁琐

【1】 UIAlertView和UIActionSheet 的使用过程

UIAlertView *alertView =[ [UIAlertView alloc] initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles: ];
;这样的形式来声明一个AlertView,UIActionSheet 类似
但是我们如果要给其中的「确认」「取消」按钮添加相应的方法,就得添加UIActionSheetDelegate或者UIAlertViewDelegate,然后实现 对应的 ClickAtButtonIndex方法,过程比较繁琐,所以在IOS8以后 apple推出了 UIAlertController将功能更加集成。

【2】 UIAlertController使用

[1]声明:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"确认?" ] preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertControllerStyle:
    #UIAlertControllerStyleActionSheet = 0,//抽屉
    #UIAlertControllerStyleAlert//警告框

这个属性区分了actionSheet和alertView

[2] 使用

  apple将alertView和actionSheet中button重新声明了一个类  UIAlertAction
  ---<1>UIAlertAction初始化
       UIAlertAction *alertAction = [UIAlertAction actionWithTitle:
                                                              style: 
                                                            handler:];
        style:
            UIAlertActionStyleDefault = 0,  //默认的风格
            UIAlertActionStyleCancel,       //取消按钮的风格
            UIAlertActionStyleDestructive   //警告的风格 (通常被用作"确认"按钮)
     handler中就是点击该Action会执行的操作
  ---<2>UIAlertAction 使用
     [ alertController addAction:alertAction ];
     使用更加便捷
  ---<3>UIAlertController使用
 [self presentViewController:alertController animated: YES completion: nil ]

相关文章

网友评论

  • 叶舞清风:一个月没敲代码了,看看,好好熟悉一下
    叶舞清风:@ManoBoo 嗯啊
    ManoBoo:@叶舞清风 我也是新手 有什么问题共同交流 加油
  • Michael_Geng:写的太浅了
    ManoBoo:@阿发的博客 恩 确实 这是简单使用 自定义控件哪些都没有写

本文标题:UIAlertController简单使用

本文链接:https://www.haomeiwen.com/subject/thjehttx.html