zoukankan      html  css  js  c++  java
  • iOS 处理方法中的可变參数

    ## iOS 处理方法中的可变參数

          近期写了一个自己定义的对话框的demo,想模仿系统的UIAlertView的实现方式。对处理可变參数的时候,遇到了小问题,于是谷歌了一下。写下了处理问题的方法。记录下来,以备后需。


    代码实现

    - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
        if (self = [super init]) {
            self.title = title;
            self.delegate = delegate;
            self.frame = CYScreen.bounds;
            // 获取可变參数的值
            if (![self isBlankString:cancelButtonTitle]) {
                [self.buttonTitles addObject:cancelButtonTitle];
            }
            NSString *str;
            va_list list;
            if(otherButtonTitles)
            {
                // 1.取得第一个參数的值
                CYLog(@"%@", otherButtonTitles);
                [self.buttonTitles addObject:otherButtonTitles];
                // 2.从第2个參数開始。依此取得全部參数的值
                va_start(list, otherButtonTitles);
                while ((str = va_arg(list, NSString *))){
                    CYLog(@"%@", str);
                    [self.buttonTitles addObject:str];
                }
                va_end(list);
            }
            CYLog(@"%@", self.buttonTitles);
        }
        return self;
    }

    方法调用

    - (IBAction)showDialog {
        CYAlertView *alert = [[CYAlertView alloc]initWithTitle:@"我的提示" message:@"消息正文" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"XXX", @"YYY", nil];
        alert.containerView = [self createDemoView];
        [alert show];
    }

    打印结果

    2015-07-06 15:54:26.422 CYCustomAlertView[358:42937] 确定
    2015-07-06 15:54:26.424 CYCustomAlertView[358:42937] XXX
    2015-07-06 15:54:26.424 CYCustomAlertView[358:42937] YYY
    2015-07-06 15:54:26.425 CYCustomAlertView[358:42937] (
        取消,
        确定,
        XXX,
        YYY,
    )
  • 相关阅读:
    随风而行(Android运动提示)
    Android简单的四则随机运算
    随手记(一)(六)
    随手记(五)
    随手记(四)
    随手记(三)
    随手记(二)
    痛点以及需求分析
    WC项目解析统计文本文件中的字符数、行数、单词数
    个人项目需求分析粉日记
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5207759.html
Copyright © 2011-2022 走看看