【问题标题】:How to define macro for UIAlertViewController?如何为 UIAlertViewController 定义宏?
【发布时间】:2016-03-30 09:14:48
【问题描述】:

在旧项目中根据需要添加了 400 个Alertview 对象,所以在这里我想在 iOS 8.0 中引入的objective-c 中为UIAlertViewController 定义macro

请帮我在常量文件中定义它。

【问题讨论】:

    标签: ios objective-c cocoa-touch ios8


    【解决方案1】:

    仅用于警报视图

    #define SHOW_ALERT(title,msg,del,cancel,other) \
    do { \
    UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:del cancelButtonTitle:cancel otherButtonTitles:other,nil]; \
    [_alert show]; \
    } while(0);
    

    仅适用于 AlertViewController

    #define SHOW_ALERT2(title,msg,ButtonTitle)\
    do { \
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];\
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:ButtonTitle\
                                                           style:UIAlertActionStyleDefault\
                                                         handler:nil]; \
        [alertController addAction:actionOk];\
        [self presentViewController:alertController animated:YES completion:nil];\
    }while(0);
    

    对于 AlertView 和 AlertViewController

    #define SHOW_ALERT3(title,msg,delegate,ButtonTitle) \
        do { \
            if ([UIAlertController class]) {\
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];\
                UIAlertAction *actionOk = [UIAlertAction actionWithTitle:ButtonTitle\
                style:UIAlertActionStyleDefault\
                handler:nil]; \
                [alertController addAction:actionOk];\
                [self presentViewController:alertController animated:YES completion:nil];\
            }\
            else {\
                UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:delegate cancelButtonTitle:ButtonTitle otherButtonTitles:nil]; \
                [_alert show]; \
            }\
        }while(0);
    

    【讨论】:

    • 我想作为 UIViewController 上的类别方法会感觉更自然。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2012-09-07
    • 2018-07-03
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多