http://stackoverflow.com/questions/1602214/use-nsarray-to-specify-otherbuttontitles

UIAlertSheet's constructor takes an otherButtonTitles parameter as a varg list. I'd like to specify the other button titles from an NSArray instead. Is this possible?

i.e. I have to do this:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: button1Title, button2Title, nil];

But since I'm generating the list of available buttons at runtime, I really want something like this:

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

Right now, I'm thinking that I need to have a seperate call to initWithTitle: for 1 item, 2 items and 3 items. Like this:

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
}

That's a lot of duplicate code, but it might actually be reasonable since I have at most three buttons. How can I avoid this?

asked Oct 21 '09 at 17:10
Steven Fisher
24.8k1489139
 

6 Answers

This is a year old but the solution is pretty simple ... do as @Simon suggested but do not specify a cancel button title, so:

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

But after adding your normal buttons, add the cancel button, like:

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

Now the key step is to specify which button is the cancel button, like:

sheet.cancelButtonIndex = [titles count];

We do [titles count] and not [titles count] - 1 because we are adding the cancel button as extra from the list of buttons in titles.

You now also specify which button you want to be the destructive button (ie the red button) by specifying the destructiveButtonIndex (typically that will be the [titles count] - 1 button). Also, if you keep the cancel button to be the last button, iOS will add that nice spacing between the other buttons and the cancel button.

All of these is iOS 2.0 compatible so enjoy.

answered Nov 28 '12 at 22:05
Ephraim
1,91111315
 
    
Not sure why it worked for you, but I had to do "[titles count] - 1" for it to work for me, iOS 7. –  Micah Mar 28 '14 at 18:38
    
[alert numberOfButtons]-1 is another way to set the cancel button's index –  Keith Apr 3 '14 at 16:42 
    
Minor note: Using the names you've established here, I think sheet.cancelButtonIndex should bealert.cancelButtonIndex, yes? –  Matt May 15 '14 at 23:59
 

Instead of adding the buttons when you initialize the UIActionSheet, try adding them with the addButtonWithTitle method using a for loop that goes through your NSArray.

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: nil]; for( NSString *title in titles)
[alert addButtonWithTitle:title];
answered Oct 21 '09 at 17:23
Simon
1,1761916
 
12  
easier than that: for (NSString * title in titles) { [alert addButtonWithTitle:title]; }There's no need to create an enumerator. –  Dave DeLong Oct 21 '09 at 17:28
3  
This makes the buttons appear AFTER the cancel button. Any way to fix this @bent or @simon? (see edit on stackoverflow.com/questions/7781022) –  Micah Hainline Oct 17 '11 at 23:38
1  
Try using the cancelButtonIndex property. –  Simon Nov 23 '11 at 20:53
1  
@MicahHainline - just add this line at the end of the 'for' loop: [actionSheet addButtonWithTitle:@"Cancel_Button_Title"]; actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1; –  Sagiftw Mar 18 '12 at 18:26 
    
This adds the buttons under the cancel button for me. –  jspooner Apr 12 '12 at 2:12

addButtonWithTitle: returns the index of the added button. Set cancelButtonTitle to nil in the init method and after adding additional buttons run this:

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
answered Aug 24 '13 at 5:21
 
- (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];
}
answered Dec 25 '12 at 7:28
 

You can add the cancel button and set it like this:

[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]];
answered Feb 18 '14 at 17:06
 

I know this is an old post, but in case someone else, like me, is trying to figure this out.

(This WAS answered by @kokemomuke. This is mostly a more detailed explanation. Also building on @Ephraim and @Simon)

It turns out the LAST entry of addButtonWithTitle: needs to be the Cancel button. I'd use:

// 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;
answered Jun 17 '14 at 7:05
 

Use NSArray to specify otherButtonTitles?的更多相关文章

  1. iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法

    今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...

  2. 不可变数组NSArray

    //数组里面不允许存放基本数据类型,只能存放“对象” NSArray *array = [NSArray arrayWithObjects:@"周星星",@"尹天仇&qu ...

  3. iOS NSArray数组过滤

    需求:在一个数组里面,将在这个数组中的并且在另一个数组里面的元素过滤掉. 即:在一个数组dataArray里面,将在dataArray数组中的并且在filteredArray数组里面的元素过滤掉. / ...

  4. NSArray 倒序 输出

    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"2",@"3" ...

  5. Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区

    Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...

  6. 一段可以清理NSArray中的空对象的代码(递归)

    - (NSArray *)clearAllNullObject{ NSMutableArray *array = [self mutableCopy]; ;i < array.count;i++ ...

  7. 一些NSArray,NSDictionary,NSSet相关的算法知识

    iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...

  8. NSArray

    版权声明:本文为博主原创文章,未经博主允许不得转载. 数组是一个元素有序.元素可重复的集合,在iOS编程中经常被用到,而Foundation框架提供了数组对象.数组对象包括不可修改的数组对象NSArr ...

  9. 如何删除NSDictionary或NSArray中的NSNull

    前段时间与某公司的技术交流,被问到一个问题,如何删除NSDictionary中的NSNull.当时在纸上写,以前太依赖Xcode编译器了,以至于方法名都写不全,最终也没写出来,我想我肯定被鄙视的体无完 ...

随机推荐

  1. 在sqlserver 中如何导出数据库表结构到excel表格中

    先建空白excel--在数据库中的左侧找到该表, 选中需要导出的数据--Ctrl+C复制--打开记事本修改编码格式为Unicode-不自动换行保存--Ctrl+A--Ctrl+C,再打开excel-- ...

  2. 二叉树 【转】http://blog.csdn.net/sjf0115/article/details/8645991

    //二叉树 #include<iostream> #include<stack> #include<queue> using namespace std; //二叉 ...

  3. 程序员必备PC维修法(软件篇)

    学会使用专业软件检测与修复电脑硬件故障问题也是程序员的一种软技能. windows篇 情景:如何获取电脑硬件的真实信息.(如何检验选购回来的硬件是否正品) 自检:使用AIDA64软件检查电脑硬件,能详 ...

  4. (转)KlayGE游戏引擎 :高效的GBUFFER管理方式

    转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=3304 个顶点.这样的数据对GPU来说是很头疼的.所以引擎往往需要在Buffer上做一些工作来 ...

  5. 【志银】Ubuntu Apache2配置SSL证书

    1.准备工作 证书文件:zain.crt.zain.key /etc/apache2/文件夹下新建ssl 文件夹,将证书文件放入/etc/apache2/ssl 2.配置SSL证书 打开/etc/ap ...

  6. Python中关于split和splitext的差别和运用

    在使用Python的过程中,在处理字符串的时候会遇到split()和os.path.split()两个函数,他们的主要区别可以概括为一个从前往后搜索字符串,后者则是从后往前搜索 '.'(reverse ...

  7. NodeJS05

    商品分类模块 分类model const mongoose = require('mongoose') const schema = new mongoose.Schema({ name: { typ ...

  8. 软工实践Alpha冲刺(6/10)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ...

  9. lua中是 ffi 解析 【是如何处理数据包的/pkt是如何传进去的】 fsfsfs

    lua中的ffi是如何解析的呢? 拿bcc中对proto的解析说起: metatype是有大学问的: ffi.metatype(ffi.typeof('struct ip_t'), { __index ...

  10. 洛谷 P4171 [JSOI2010]满汉全席 解题报告

    P4171 [JSOI2010]满汉全席 题目描述 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高 ...