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
}

这是很多重复的代码,但它实际上可能是合理的,因为我最多有三个按钮。我怎么能避免这个?

有帮助吗?

解决方案

这是一年了,但解决方案非常简单......按照@Simon的建议做,但不指定取消按钮标题,所以:

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: nil
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

但是在添加普通按钮后,添加取消按钮,如:

for( NSString *title in titles)  {
    [alert addButtonWithTitle:title]; 
}

[alert addButtonWithTitle:cancelString];

现在关键步骤是指定哪个按钮是取消按钮,例如:

alert.cancelButtonIndex = [titles count];

我们 [titles count] 而不是 [titles count] - 1 因为我们在标题中的按钮列表中添加了取消按钮作为额外的

现在,您还可以通过指定destructiveButtonIndex(通常是 [titles count] - 1 按钮)指定您想要成为破坏性按钮的按钮(即红色按钮)。此外,如果您将取消按钮保留为最后一个按钮,iOS将在其他按钮和取消按钮之间添加漂亮的间距。

所有这些都与iOS 2.0兼容,所以尽情享受。

其他提示

不是在初始化UIActionSheet时添加按钮,而是使用通过NSArray的for循环,使用addButtonWithTitle方法添加它们。

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: cancelString
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

for( NSString *title in titles)  
    [alert addButtonWithTitle:title]; 

addButtonWithTitle:返回添加按钮的索引。在init方法中将cancelButtonTitle设置为nil,并在添加其他按钮后运行:

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
- (void)showActionSheetWithButtons:(NSArray *)buttons withTitle:(NSString *)title {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: title 
                                                             delegate: self
                                                    cancelButtonTitle: nil 
                                               destructiveButtonTitle: nil 
                                                    otherButtonTitles: nil];

    for (NSString *title in buttons) {
        [actionSheet addButtonWithTitle: title];
    }

    [actionSheet addButtonWithTitle: @"Cancel"];
    [actionSheet setCancelButtonIndex: [buttons count]];
    [actionSheet showInView:self.view];
}

您可以添加取消按钮并将其设置为:

[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]];

我知道这是一个老帖子,但万一其他人,像我一样,正试图解决这个问题。

(这是由@kokemomuke回答。这主要是一个更详细的解释。还建立在@Ephraim和@Simon上)

结果是addButtonWithTitle的最后条目:需要是 Cancel 按钮。我会用:

// All titles EXCLUDING Cancel button
for( NSString *title in titles)  
    [sheet addButtonWithTitle:title];


// The next two line MUST be set correctly: 
// 1. Cancel button must be added as the last entry
// 2. Index of the Cancel button must be set to the last entry

[sheet addButtonWithTitle:@"Cancel"];

sheet.cancelButtonIndex = titles.count - 1;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top