UIAlertController的使用

前言

在ios8.3中,UIAlertViewUIActionSheet已经被列为不建议使用的范畴,按照苹果果断的性格,两个大版本之后就会被淘汰,现在建议使用UIAlertController去替代这两个方法。


回顾UIAlertView与UIActionSheet

  1. 创建方法
    UIAlertView 创建方法
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title"
                                                   message:@"alert message"
                                                  delegate:self
                                         cancelButtonTitle:@"ok"
                                         otherButtonTitles:@"one", @"two", nil];
   [alert show];

UIActionSheet 创建方法

UIActionSheet *actionSheet
          = [[UIActionSheet alloc] initWithTitle:@"title"
                                        delegate:self
                               cancelButtonTitle:@"cancel"
                          destructiveButtonTitle:@"destructive"
                               otherButtonTitles:@"one", @"two", nil];
          [actionSheet showInView:self.view.window];

有次可见UIActionSheet与UIAlertView创建方法几乎完全相同。


2 . 代理
UIAlertView的代理经常会随着UIAlertView的创建一并用到,而它最常用的代理应该是在点击UIAlertView中button所要响应事件的代理。

//点击时
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
        //读取名字
        NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
        if ([buttonTitle isEqualToString:@"one"]) {
          
        }
    
}

在点击UIAlertView上面的button,根据button所在的位置得到button的信息,比如名字,再在确认后实现方法。


UIAlertController

1 .创建UIAlertController方法

UIAlertController *uiAlertController=
 [UIAlertController alertControllerWithTitle:@"title"
 message:@"message"
 preferredStyle:UIAlertControllerStyleAlert];
 [self presentViewController:uiAlertController animated:YES completion:nil];

顾名思义,title与message与之前一样,展示方式有所不同,更主要的是现在创建的时候多了一个preferredStyle属性。

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert } NS_ENUM_AVAILABLE_IOS(8_0);

在preferredStyle中一共有两种方法,UIAlertControllerStyleActionSheet与UIAlertControllerStyleAlert,分别取代了UIActionSheet和UIAlertView。

UIAlertControllerStyleAlert示例

UIAlertControllerStyleActionSheet示例

可以看出,创建的UIAlertController与 没有 button的UIActionSheet和UIAlertView是可以完全替换的,现在怎样在UIAlertController中添加button呢?这时候我们需要用到UIAlertAction。


UIAlertAction

2 .添加UIAlertAction

(以下UIAlertAction都不以特定的方式展示)

  • 创建UIAlertAction
  UIAlertAction *action=[UIAlertAction actionWithTitle:@"title" style:UIAlertActionStyleDefault handler:nil];
    [uiAlertController addAction:action];

创建UIAlertAction中,title就是按钮的名字,style是指UIAlertAction的类型,handler就是点击按钮时所要处理的事件。
最后最重要的是要想在UIAlertController中添加UIAlertAction,就需要实现addAction方法。

  • UIAlertActionStyle
    与UIAlertController一样,UIAlertAction的Style也不是唯一的。
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    UIAlertActionStyleDefault = 0,
    UIAlertActionStyleCancel,
    UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);

一共有三个,分别是UIAlertActionStyleDefault(默认),UIAlertActionStyleCancel(取消),UIAlertActionStyleDestructive(重要)。
分别试试不同style的效果。

UIAlertActionStyleDefault与UIAlertActionStyleCancel:


UIAlertActionStyleDestructive:


在点击按钮之后UIAlertController一样都会消失。


  • UIAlertAction的顺序
    UIAlertAction的顺序只根据在UIAlertController添加的次序排序,与 UIAlertAction创建时间顺序无关。(UIAlertActionStyleCancel的UIAlertAction的列外,待会会讲到)
    例一:
 UIAlertAction *action1=[UIAlertAction actionWithTitle:@"title1" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2=[UIAlertAction actionWithTitle:@"title2" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *action3=[UIAlertAction actionWithTitle:@"title3" style:UIAlertActionStyleDefault handler:nil];
    [uiAlertController addAction:action1];
    [uiAlertController addAction:action2];
    [uiAlertController addAction:action3];

结果:


例二:

 UIAlertAction *action1=[UIAlertAction actionWithTitle:@"title1" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2=[UIAlertAction actionWithTitle:@"title2" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *action3=[UIAlertAction actionWithTitle:@"title3" style:UIAlertActionStyleDefault handler:nil];
    [uiAlertController addAction:action3];
    [uiAlertController addAction:action2];
    [uiAlertController addAction:action1];

结果:


6.png-19.3kB
6.png-19.3kB

  • UIAlertActionStyleCancel

UIAlertActionStyleCancel是UIAlertActionStyle中最特殊的一个。

第一,任何类型的UIAlertViewController中,最多只能有一个UIAlertActionStyleCancel类型的UIAlertAction,如果超过一个,则会报错。


第二,UIAlertActionStyleCancel类型的UIAlertAction与其他类型的按钮位置不同,在UIAlertViewController中,UIAlertAction只有等于两个时,UIAlertActionStyleCancel的位置按着苹果官方设定,会在左边。
代码:

  UIAlertAction *action2=[UIAlertAction actionWithTitle:@"title2" style:UIAlertActionStyleDefault handler:nil];
   UIAlertAction *action3=[UIAlertAction actionWithTitle:@"title3" style:UIAlertActionStyleCancel handler:nil];
   [uiAlertController addAction:action3];
   [uiAlertController addAction:action2];

截图:


在其他情况中,UIAlertActionStyleCancel类型的UIAlertAction的位置永远是最底层。
截图1:



截图2:



  • 实现UIAlertAction中的方法
UIAlertAction *action3=[UIAlertAction actionWithTitle:@"title3" style:UIAlertActionStyleCancel handler:^ (UIAlertAction *action){
       NSLog(@"title3 test ");
   }];

只需要添加一段block即可。
点击后实现方法:



总结

UIAlertController将以前的方式用模块化实现,并不需要再使用代理方法去实现功能,定位上也更加灵活。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容