標籤:
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
completionBlock:(void (^)(NSUInteger buttonIndex, EMAlertView *alertView))block
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... {
//#import <objc/runtime.h>標頭檔
//objc_setAssociatedObject需要四個參數:來源物件,關鍵字,關聯的對象和一個關聯策略。
objc_setAssociatedObject(self, "blockCallback", [block copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil])
{
if (cancelButtonTitle) {
[self addButtonWithTitle:cancelButtonTitle];
self.cancelButtonIndex = [self numberOfButtons] - 1;
}
id eachObject;
va_list argumentList;//va_list 是一個字元指標,可以理解為指向當前參數的一個指標,取參必須通過這個指標進行
if (otherButtonTitles) {
[self addButtonWithTitle:otherButtonTitles];
va_start(argumentList, otherButtonTitles);// 然後應該對argumentList進行初始化,讓它指向可變參數表裡面的第一個參數,這是通過 va_start 來實現的,第一個參數是argumentList本身,第二個參數是在變參表前面緊挨著的一個變數,即“...”之前的那個參數
while ((eachObject = va_arg(argumentList, id))) {//然後是擷取參數,調用va_arg,它的第一個參數是argumentList,第二個參數是要擷取的參數的指定類型,然後返回這個指定類型的值,並且把argumentList的位置指向變參表的下一個變數位置
[self addButtonWithTitle:eachObject];
}
va_end(argumentList);//置空argumentList//擷取所有的參數之後,我們有必要將這個argumentList指標關掉,以免發生危險,方法是調用 va_end,它使輸入的參數argumentList置為 NULL,應該養成擷取完參數表之後關閉指標的習慣。說白了,就是讓我們的程式具有健壯性。通常va_start和va_end是成對出現。
}
}
return self;
}
iOS va_list,va_start,va_end