【发布时间】:2010-12-08 19:14:59
【问题描述】:
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
}
这是很多重复的代码,但实际上它可能是合理的,因为我最多有三个按钮。我怎样才能避免这种情况?
【问题讨论】:
标签: iphone nsarray uialertsheet