Variable parameters in iOS Processing Method
# Variable parameters in iOS Processing Methods
I recently wroteCustom dialog box
To simulate the implementation of the system's UIAlertView.Variable parameters
I encountered a small problem, So Google wrote down the method to solve the problem and recorded it for future use.
Code Implementation
-(Instancetype) initWithTitle :( NSString *) title message :( NSString *) message delegate :( id) delegate cancelButtonTitle :( NSString *) cancelButtonTitle otherButtonTitles :( NSString *) conflict ,... {if (self = [super init]) {self. title = title; self. delegate = delegate; self. frame = CYScreen. bounds; // get the variable parameter value if (! [Self isBlankString: cancelButtonTitle]) {[self. buttonTitles addObject: cancelButtonTitle];} NSString * str; va_list list; if (otherButtonTitles) {// 1. get the CYLog (% @, otherButtonTitles) of the first parameter; [self. buttonTitles addObject: otherButtonTitles]; // 2. starting from 2nd parameters, obtain the values of all parameters va_start (list, otherButtonTitles); while (str = va_arg (list, NSString *))) {CYLog (@ % @, str); [self. buttonTitles addObject: str];} va_end (list);} CYLog (% @, self. buttonTitles);} return self ;}
Method call
-(IBAction) showDialog {CYAlertView * alert = [[CYAlertView alloc] initWithTitle: @ My prompt message: @ message body delegate: self cancelButtonTitle: @ cancel otherButtonTitles: @ OK, @ XXX, @ YYY, nil]; alert. containerView = [self createDemoView]; [alert show];}
Print results
15:54:26. 422 CYCustomAlertView [358: 42937] OK 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] (cancel, OK, XXX, YYY ,)