我正在尝试将UIAlertView子类化以更好地处理我的应用程序中的错误状态。问题在于otherButtonTitles nil终止参数,当我创建我的子类时,它只选择列表中的第一个字符串,而不是所有字符串
+ (ErrorAlertView *)displayErrorAlertViewWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id<UIAlertViewDelegate>)delegate
errorDelegate:(id<ErrorAlertViewDelegate>)errorDelegate
cancelButtonTitle:(NSString *)cancelButtonTitle
displayNow:(BOOL)displayNow
tag:(NSUInteger)tag
otherButtonTitles:(NSString *)otherButtonTitles, ...;
{
ErrorAlertView *errorAlertView = [[ErrorAlertView alloc] initWithTitle:title
message:message
delegate:delegate
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles, nil];
errorAlertView.errorDelegate = errorDelegate;
errorAlertView.tag = tag;
if (displayNow) {
[errorAlertView show];
}
return [errorAlertView autorelease];
}
如果我进行以下调用上述方法:
[ErrorAlertView displayErrorAlertViewWithTitle:[self noInternetConnectionAlertViewTitle]
message:[self noInternetConnectionAlertViewMessage]
delegate:self
errorDelegate:errorDelegate
cancelButtonTitle:@"OK"
displayNow:YES
tag:ErrorAlertTagInternetConnectionError
@"Try again",@"Setting", nil];
UIAlertView仅显示@“重试”按钮。
最佳答案
您不能发送像这样的可变参数集。
当我将UIAlertView子类化时,我这样做:
va_list args;
va_start(args, otherButtonTitles);
for (NSString *anOtherButtonTitle = otherButtonTitles; anOtherButtonTitle != nil; anOtherButtonTitle = va_arg(args, NSString*)) {
[self addButtonWithTitle:anOtherButtonTitle];
}
或者,您可以创建函数的变体,该变体接受va_list作为(单个)参数,然后运行上面的代码。
通常,在编写可变参数函数时,应包括一个替代方法来处理这种情况。在这种情况下,Apple提供了addButtonWithTitle:方法。
关于objective-c - 子类化UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8309869/