iOS 處理方法中的可變參數

來源:互聯網
上載者:User

iOS 處理方法中的可變參數
## iOS 處理方法中的可變參數

最近寫了一個自訂的對話方塊的demo,想模仿系統的UIAlertView的實現方式,對處理可變參數的時候,遇到了小問題,於是Google了一下,寫下了處理問題的方法,記錄下來,以備後需。

代碼實現
- (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] XXX2015-07-06 15:54:26.424 CYCustomAlertView[358:42937] YYY2015-07-06 15:54:26.425 CYCustomAlertView[358:42937] (    取消,    確定,    XXX,    YYY,)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.