objective-c - 子类化UIAlertView

标签 objective-c ios uialertview null subclassing

我正在尝试将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/

相关文章:

objective-c - 在我的 iphone 应用程序中,我想提供导入 Facebook、gmail 联系人的功能

iphone - 在我的应用程序中复制 iPhone 库的音乐文件

objective-c - NSTextField 在哪里?

cocoa-touch - UIWebView 中的 Cookie

iphone - 可达性中的 UIAlertView 异常

ios - 如何在 UIAlertController 中设置两个 UITextField 之间的距离?

objective-c - 使用 arc 退出对象的初始化?

ios - 获取 SSL 证书详细信息

ios - 了解iOS中的图片大小

swift - 带输入密码的警报 View