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];
现在,我想我需要为1个项目、2个项目和3个项目分别调用initWithTitle:
。如下所示:
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
}
这有很多重复的代码,但它实际上可能是合理的,因为我最多有三个按钮。我怎样才能避免这种情况?
https://stackoverflow.com/questions/1602214
复制相似问题