一个UIAlertView中前进可变参数 [英] Forward variadic arguments for a UIAlertView

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

问题描述

我想建立一个非常简单的 UIAlertView中用文本编辑,一个确定和取消按钮,我想禁用的基础上的OK按钮文本编辑的内容。

I'm trying to set up a very simple UIAlertView with a text edit, an Ok and a cancel button, and I want to disable the Ok button based on the content of the text edit.

要能够保留委托让他不会在警报视图前走开(因此只要用户不与警报视图东西导致崩溃),我子类它。现在,我希望能够从我的init方法的 UIAlertView中 init方法转发 otherButtonTitles 的说法,但对于由于种种原因,只是这样做:

To be able to retain the delegate so that he doesn't go away before the alert view (and thus cause a crash as soon as the user does something with the alert view), I've subclassed it. Now, I want to be able to forward the otherButtonTitles argument from my init method to the UIAlertView init method, but for some reasons, simply doing that:

- (id)initWithTitle:(NSString *)title
            message:(NSString*)message
           delegate:(id /*<UIAlertViewDelegate>*/)delegate
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles, ... {

    if (self = [super initWithTitle:title 
                            message:message 
                           delegate:delegate 
                  cancelButtonTitle:cancelButtonTitle 
                  otherButtonTitles:otherButtonTitles, nil]) {
        //stuff
    }

只增加了args来的第一个元素的警报视图的。我发现,其实我可以手动按钮使用此添加到警报视图:

only adds the first element of the args to the alert view. I've found that I can actually manually add the buttons to the alert view using this:

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

但后来,我的 alertViewShouldEnableFirstOtherButton 委托方法不再被调用, < STRONG>在这个职位可能的解释

but then, my alertViewShouldEnableFirstOtherButton delegate method isn't called anymore, with the probable explanation in this post.

因此​​,我怎么能期待我的正确 otherButtonTitles 来正确 UIAlertView中 init方法?

Thus, how can I forward correctly my otherButtonTitles to the UIAlertView init method correctly?

推荐答案

让我们减少击键那么:

NSMutableArray *otherButtonTitles = [NSMutableArray array];
// .. collect varargs into ^^^

#define T(n) ([otherButtonTitles objectAtIndex:n])
#define CASE(n, ...) case n: self = [super initWithTitle:title \
                                                 message:message \ 
                                                delegate:delegate \
                                       cancelButtonTitle:cancelButtonTitle \
                                       otherButtonTitles:__VA_ARGS__, nil]; \
                             break

switch ([otherButtonTitles count]) {
    CASE(0, nil);
    CASE(1, T(0));
    CASE(2, T(0), T(1));
    CASE(3, T(0), T(1), T(2));
    // ... repeat until bored ...
    default: @throw @"too many buttons"; // or return nil
}

这篇关于一个UIAlertView中前进可变参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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