UIAlertSheet 的构造函数将 otherButtonTitles 参数作为 varg 列表。我想从 NSArray 指定其他按钮标题。这可能吗?
即我必须这样做:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: button1Title, button2Title, nil];
但由于我在运行时生成可用按钮列表,所以我真的想要这样的东西:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: otherButtonTitles];
现在,我在想我需要单独调用initWithTitle:
1 个项目、2 个项目和 3 个项目。像这样:
if ( [titles count] == 1 ) {
alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: [titles objectAtIndex: 0], nil];
} else if ( [titles count] == 2) {
alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: [titles objectAtIndex: 0], [titles objectAtIndex: 1], nil];
} else {
// and so on
}
这是很多重复的代码,但实际上可能是合理的,因为我最多有三个按钮。我怎样才能避免这种情况?