首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过传入数组而不是UIActionSheet来创建otherButtons

通过传入数组而不是UIActionSheet来创建otherButtons
EN

Stack Overflow用户
提问于 2010-03-05 09:47:37
回答 4查看 27.9K关注 0票数 110

我有一个字符串数组,希望用于UIActionSheet上的按钮标题。不幸的是,方法调用中的otherButtonTitles:参数接受可变长度的字符串列表,而不是数组。

那么我如何才能将这些标题传递到UIActionSheet中呢?我建议的解决方法是将nil传入otherButtonTitles:,然后使用addButtonWithTitle:分别指定按钮标题。但这样做的问题是将"Cancel“按钮移动到UIActionSheet上的第一个位置,而不是最后一个位置;我希望它是最后一个。

有没有办法1)传递一个数组来代替字符串的变量列表,或者2)把cancel按钮移到UIActionSheet的底部?

谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-03-05 12:51:14

我让它起作用了(你只需要,用一个普通的按钮就可以了,只需要把它添加到下面:

代码语言:javascript
复制
NSArray *array = @[@"1st Button",@"2nd Button",@"3rd Button",@"4th Button"];

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    // ObjC Fast Enumeration
    for (NSString *title in array) {
        [actionSheet addButtonWithTitle:title];
    }

    actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];

    [actionSheet showInView:self.view];
票数 249
EN

Stack Overflow用户

发布于 2010-06-24 06:31:41

注意: actionSheet addButtonWithTitle:返回该按钮的索引,所以为了安全和“干净”,您可以这样做:

代码语言:javascript
复制
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
票数 78
EN

Stack Overflow用户

发布于 2014-05-05 02:10:09

采用Jaba和Nick的答案,并将其进一步扩展。要将销毁按钮合并到此解决方案中,请执行以下操作:

代码语言:javascript
复制
// Create action sheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];
// Action Buttons
for (NSString *actionName in actionNames){
    [actionSheet addButtonWithTitle: actionName];
}

// Destruction Button
if (destructiveName.length > 0){
    [actionSheet setDestructiveButtonIndex:[actionSheet addButtonWithTitle: destructiveName]];
}

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

// Present Action Sheet
[actionSheet showInView: self.view];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2384044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档