UIAlertSheet的构造函数将otherButtonTitles参数作为varg列表.我想指定NSArray中的其他按钮标题.这可能吗?
即我必须这样做:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: button1Title, button2Title, nil];
Run Code Online (Sandbox Code Playgroud)
但由于我在运行时生成可用按钮列表,我真的想要这样的东西:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: otherButtonTitles];
Run Code Online (Sandbox Code Playgroud)
现在,我想我需要单独拨打一个initWithTitle:项目,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 …Run Code Online (Sandbox Code Playgroud) 在python中,很容易构建一个字典或数组,并将其解压缩到具有可变参数的函数
我有这个:
- (BOOL) executeUpdate:(NSString*)sql, ... {
Run Code Online (Sandbox Code Playgroud)
手动方式是这样的:
[db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
@"hi'", // look! I put in a ', and I'm not escaping it!
[NSString stringWithFormat:@"number %d", i],
[NSNumber numberWithInt:i],
[NSDate date],
[NSNumber numberWithFloat:2.2f]];
Run Code Online (Sandbox Code Playgroud)
但我不能硬编码我正在调用的参数,我想:
NSMutableArray *values = [NSMutableArray array];
for (NSString *fieldName in props) {
..
..
[values addObject : value]
}
[db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, …Run Code Online (Sandbox Code Playgroud) 我已经子类化了UIActionSheet,在-init方法中,我必须在调用super之后单独添加按钮init(不能传递var_args).
现在,它看起来像这样:
if (self = [super initWithTitle:title delegate:self cancelButtonTitle:cancel destructiveButtonTile:destroy otherButtonTitles:firstButton,nil]) {
if (firstButton) {
id buttonTitle;
va_list argList;
va_start(argList, firstButtton);
while (buttonTitle = va_arg(argList, id)) {
[self addButtonWithTitle:buttonTitle]
}
va_end(argList);
}
}
return self;
Run Code Online (Sandbox Code Playgroud)
但是,我在这种情况下的具体用途没有破坏性按钮,取消按钮和其他四个按钮.当它出现时,订单全部关闭,显示为
Button1
取消
Button2
Button3
就像他们被简单地添加到列表的末尾,这是有道理的; 但是,我不希望它看起来像这样; 那我该怎么办?事实上,是否有任何方法可以UIActionSheet正确地进行子类化并使其工作?