UIiOSiOS进阶指南

UIAlertController简单使用

2015-12-07  本文已影响451人  ManoBoo

在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 ]
上一篇下一篇

猜你喜欢

热点阅读