初期化メソッドのオーバーライド
可変引数はあとから -[addButtonWithTitle:]で追加してやる。
- (id)initWithTitle:(NSString *)title
delegate:(id < UIActionSheetDelegate >)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)firstOtherTitle,...
{
self = [super initWithTitle:title
delegate:delegate
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
if (self) {
int index = 0;
if (destructiveButtonTitle) {
[self addButtonWithTitle:destructiveButtonTitle];
self.destructiveButtonIndex = index;
index++;
}
if (firstOtherTitle) {
[self addButtonWithTitle:firstOtherTitle];
index++;
va_list args;
va_start(args, firstOtherTitle);
NSString* title;
while (title = va_arg(args, NSString*)) {
[self addButtonWithTitle:title];
index++;
}
va_end(args);
}
[self addButtonWithTitle:cancelButtonTitle];
self.cancelButtonIndex = index;
}
return self;
}試しに呼び出すとこんな感じ。ちゃんと動いているようだ。
なお self=[super ...] で cancel と destructive ボタンを指定すると順番がおかしくなる。
これを防ぐために cancel と destructive ボタンも標準の順番に合うように追加している。
参考情報
- UIActionSheet addButtonWithTitle: doesn't add buttons in the right order - Stack Overflow
- 今回の方法が載っていた
- CodeResource - Uncategorized Messages - Uiactionsheet And Popviewcontrolleranimated
- 同様の方法が示されていた
- Cocoa with Love: Variable argument lists in Cocoa
- Cocoaにおける可変引数の記事
- Cocoaの日々 - 2005年1月
- 当ブログでも大昔に取り上げたことがあった。
- UIActionSheet Class Reference
- リファレンス
- - - -
次回は UIActionSheet の Blocks化をやります(今回はその伏線)。





Responses
Leave a Response