我有一个字符串数组,希望用于UIActionSheet上的按钮标题。不幸的是,方法调用中的otherButtonTitles:参数接受可变长度的字符串列表,而不是数组。
那么我如何才能将这些标题传递到UIActionSheet中呢?我建议的解决方法是将nil传入otherButtonTitles:,然后使用addButtonWithTitle:分别指定按钮标题。但这样做的问题是将"Cancel“按钮移动到UIActionSheet上的第一个位置,而不是最后一个位置;我希望它是最后一个。
有没有办法1)传递一个数组来代替字符串的变量列表,或者2)把cancel按钮移到UIActionSheet的底部?
谢谢。
发布于 2010-03-05 12:51:14
我让它起作用了(你只需要,用一个普通的按钮就可以了,只需要把它添加到下面:
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];
发布于 2010-06-24 06:31:41
注意: actionSheet addButtonWithTitle:返回该按钮的索引,所以为了安全和“干净”,您可以这样做:
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
发布于 2014-05-05 02:10:09
采用Jaba和Nick的答案,并将其进一步扩展。要将销毁按钮合并到此解决方案中,请执行以下操作:
// 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];
https://stackoverflow.com/questions/2384044
复制相似问题