首页 > 代码库 > iOS8新特性之UIAlertController

iOS8新特性之UIAlertController

iOS8把很多相识的控件结合在一起。

比如这节要讲的:UIAlertController

UIAlertController 结合了UIAlert/UIActionSheet的所有功能

// 创建代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"啊啊啊啊啊啊" preferredStyle:UIAlertControllerStyleAlert];
<p class="p2"><span class="s1">// modal控制器</span></p><p class="p2"><span class="s1">[</span><span class="s2">self</span><span class="s1"> </span>presentViewController<span class="s1">:alert </span>animated<span class="s1">:</span><span class="s2">YES</span><span class="s1"> </span>completion<span class="s1">:</span><span class="s2">nil</span><span class="s1">];</span></p>

    

preferredStyle 是个枚举
<p class="p1"><span class="s1">typedef</span> <span class="s1">NS_ENUM</span>(NSInteger, UIAlertControllerStyle) {</p><p class="p1">    UIAlertControllerStyleActionSheet = <span class="s2">0</span>,</p><p class="p1">    UIAlertControllerStyleAlert</p><p class="p2"><span class="s3">} </span>NS_ENUM_AVAILABLE_IOS<span class="s3">(</span><span class="s2">8</span><span class="s3">_0);</span></p>


但是,这样子弹出的框是没有点击按钮的。

所以,必须添加按钮事件

<pre name="code" class="objc">        // UIAlertActionStyleCancel 是个类型枚举
//        typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
//            UIAlertActionStyleDefault = 0,
//            UIAlertActionStyleCancel,
//            UIAlertActionStyleDestructive
//        } NS_ENUM_AVAILABLE_IOS(8_0);

UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        // 回调在block里面
        // to do..
    }];
    [alert addAction:action];
    
    
    UIAlertAction *actionDelete = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        // to do..
    }];
    [alert addAction:actionDelete];

添加按钮可以了,但是添加输入框要怎么办呢?

类提供了一个方法:

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"11";
    }];
可以在block里面设置textField任意的属性,更加灵活

@property (nonatomicreadonlyNSArray *textFields; // 获取所有的输入框


今天就先分享到这里了~~


iOS8新特性之UIAlertController