SDK中有许多方法要求输入字符串列表,以nil结尾,例如,在UIActionSheet中:
- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,'otherButtonTitles'是以nil结尾的NSStrings列表.我想要做的是使用NSStrings的构造NSMutableArray调用此方法,因为我想动态创建和排序参数.我该怎么做?在这种情况下,我不确定如何创建一个nil终止指向NSStrings的指针,如果传入它甚至可以工作.我是否必须手动为其分配内存并将其释放?
您无法将任何数组转换为可变参数列表.
但是,对于UIActionSheet,您可以在创建工作表后使用添加其他按钮标题-addButtonWithTitle:
UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:...
/*etc*/
otherButtonTitles:nil];
for (NSString* otherButtonTitle in otherButtonTitlesArray)
{
[sheet addButtonWithTitle:otherButtonTitle];
}
Run Code Online (Sandbox Code Playgroud)
我还需要制作动态行动表.所以我做了一个大部分空的动作表.添加了我的按钮.然后添加"取消"按钮并将其标记为取消.
sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:homeName, nil];
[sheet addButtonWithTitle:otherButton1];
[sheet addButtonWithTitle:otherButton2];
[sheet addButtonWithTitle:@"Cancel"];
[sheet setCancelButtonIndex:[sheet numberOfButtons] - 1];
sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[sheet showInView:self.view];
[sheet release];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1784 次 |
| 最近记录: |