美文网首页
十一、iWatch常用控件基本用法之WKAlertAction

十一、iWatch常用控件基本用法之WKAlertAction

作者: Dosun | 来源:发表于2017-03-14 11:32 被阅读105次

WKAlertAction 对象展示按钮和包含其点击事件,当创建提示行为,可以自定义标题和按钮样式以及用户点击按扭的事件。

一、简介

a. 创建WKAlertAction 对象请用方法
参数说明:
每一个参数:WKAlertAction的标题。
第二个参数:style是WKAlertAction的样式,总共有三种样式。
a. WKAlertActionStyleDefault 默认样式
b. WKAlertActionStyleCancel 位置在屏幕的左上角
c. WKAlertActionStyleDestructive 字体是红色
第三个参数:handler是一个block回调,当用户点击AlertAction时,会调用其对应的方法。

+ (instancetype)actionWithTitle:(NSString *)title style:(WKAlertActionStyle)style handler:(WKAlertActionHandler)handler;

b. 展示WKAlertAction 请用如下方法
第一个参数:展示提示框的标题
第二个参数:提示框的内容
第三个参数:提示框展示的样式,其中preferredStyle 有三种样式
a. WKAlertControllerStyleAlert 一般样式展示;
b. WKAlertControllerStyleSideBySideButtonsAlert 必须只有 2个 action,否则没有效果;
c. WKAlertControllerStyleActionSheet 表的左上角一个cancel;
第四个参数:WKAlertAction数组。

- (void)presentAlertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(WKAlertControllerStyle)preferredStyle actions:(NSArray <WKAlertAction *>*)actions;

二、代码

#import "InterfaceController.h"
@interface InterfaceController()

@end

@implementation InterfaceController
- (IBAction)showAlert {
    
    //1.定义数组WKAlertAction
    NSMutableArray<WKAlertAction*> *mArr = [NSMutableArray array];
    
    //2.定义 AlertAction
    WKAlertAction *action1 = [WKAlertAction actionWithTitle:@"1" style:WKAlertActionStyleDefault handler:^{
        NSLog(@"你好,你点击第一个button");
    }];
    
    WKAlertAction *action2 = [WKAlertAction actionWithTitle:@"2" style:WKAlertActionStyleCancel handler:^{
        NSLog(@"你好,你点击第二个button");
    }];
    
    //3. 添加action
    [mArr addObject:action1];
    [mArr addObject: action2];
    
    //4.展示alertAction
    [self presentAlertControllerWithTitle:@"标题" message:@"" preferredStyle:WKAlertControllerStyleSideBySideButtonsAlert actions:mArr];
}

@end

最后提醒一次,展示WKAlertAction时,用WKAlertControllerStyleSideBySideButtonsAlert样式时,要注意其只能加入两个action,如果多于二个的话,不会出现alert 提醒。
Over!!! Thanks!

相关文章

网友评论

      本文标题:十一、iWatch常用控件基本用法之WKAlertAction

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