对UIAlertView进行子类化 [英] Subclassing UIAlertView

查看:139
本文介绍了对UIAlertView进行子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将UIAlertView子类化以更好地处理我的应用中的错误状态。我遇到的麻烦是使用 otherButtonTitles nil终止参数,当我创建我的子类时,它只是拾取列表中的第一个字符串而不是所有字符串

Am attempting to subclass UIAlertView to better handle error states in my app. The trouble am having is with the otherButtonTitles nil terminated parameter, when I create my subclass it is only picking up the first string in the list rather than all the strings

+ (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];


}

如果我进行以下调用以上方法:

If I make the following call the above method:

[ErrorAlertView displayErrorAlertViewWithTitle:[self noInternetConnectionAlertViewTitle] 
                                           message:[self noInternetConnectionAlertViewMessage] 
                                          delegate:self 
                                     errorDelegate:errorDelegate 
                                 cancelButtonTitle:@"OK" 
                                        displayNow:YES 
                                               tag:ErrorAlertTagInternetConnectionError 
                                 @"Try again",@"Setting", nil];

UIAlertView只显示@再试一次按钮。

The UIAlertView is being shown with only the @"Try again" button being shown.

推荐答案

你不能发送像这样的变量参数集。

You can't send a variable set of parameters like that.

我将UIAlertView子类化了我做了这个:

When I subclassed UIAlertView I did this:

va_list args;
va_start(args, otherButtonTitles);
for (NSString *anOtherButtonTitle = otherButtonTitles; anOtherButtonTitle != nil; anOtherButtonTitle = va_arg(args, NSString*)) {
    [self addButtonWithTitle:anOtherButtonTitle];
}

或者,您可以创建一个函数变体,接受va_list作为(单个)参数,然后运行上面的代码。

Alternatively, you could create a variant of your function that accepts a va_list as a (single) parameter, and then runs the above code.

通常,在编写可变参数函数时,应该包含一个替代方法来处理这种可能性。在这种情况下,Apple提供了addButtonWithTitle:方法。

Generally, when writing a variadic function, you should include an alternative for handling this eventuality. Apple supply the addButtonWithTitle: method in this case.

这篇关于对UIAlertView进行子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆