wil*_*amb 1 null objective-c subclassing uialertview ios
我试图继承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];
}
Run Code Online (Sandbox Code Playgroud)
如果我进行以下调用上面的方法:
[ErrorAlertView displayErrorAlertViewWithTitle:[self noInternetConnectionAlertViewTitle]
message:[self noInternetConnectionAlertViewMessage]
delegate:self
errorDelegate:errorDelegate
cancelButtonTitle:@"OK"
displayNow:YES
tag:ErrorAlertTagInternetConnectionError
@"Try again",@"Setting", nil];
Run Code Online (Sandbox Code Playgroud)
显示UIAlertView时只显示@"再试一次"按钮.
Luk*_*uke 14
子类注释
UIAlertView类旨在按原样使用,不支持子类化.此类的视图层次结构是私有的,不得修改.
但是,您可能会发现许多其他警报视图实现在CocoaControls上发布.
您不能发送这样的变量参数集.
当我将UIAlertView子类化时,我这样做了:
va_list args;
va_start(args, otherButtonTitles);
for (NSString *anOtherButtonTitle = otherButtonTitles; anOtherButtonTitle != nil; anOtherButtonTitle = va_arg(args, NSString*)) {
[self addButtonWithTitle:anOtherButtonTitle];
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以创建函数的变体,将va_list作为(单个)参数接受,然后运行上面的代码.
通常,在编写可变参数函数时,应该包含一个替代方法来处理这种可能性.在这种情况下,Apple提供了addButtonWithTitle:方法.
| 归档时间: |
|
| 查看次数: |
5188 次 |
| 最近记录: |