argumentList只显示一个

时间:2013-04-09 09:31:09

标签: iphone ios cocoa

我尝试了以下代码,但只显示otherButtonTitles中的第一个。

- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [self init];
    if (self)
    {
        // Build normal buttons
        va_list argumentList;
        va_start(argumentList, otherButtonTitles);

        NSString *argString = otherButtonTitles;

        while (argString != nil)
        {
            UIButton *button = [self buildButtonWithTitle:argString];

            [self.buttons addObject:button];

            argString = va_arg(argumentList, NSString *);
        }

        va_end(argumentList);

        // Build cancel button
        UIButton *cancelButton = [self buildCancelButtonWithTitle:cancelButtonTitle];
        [self.buttons insertObject:cancelButton atIndex:0];

        // Add primary button
        if (primaryButtonTitle)
        {
            UIButton *primaryButton = [self buildPrimaryButtonWithTitle:primaryButtonTitle];
            [self.buttons addObject:primaryButton];
        }

        // Add destroy button
        if (destructiveButtonTitle)
        {
            UIButton *destroyButton = [self buildDestroyButtonWithTitle:destructiveButtonTitle];
            [self.buttons insertObject:destroyButton atIndex:1];
        }
    }

    return self;
}

如何修改?

1 个答案:

答案 0 :(得分:1)

这是我为你写的一般模板。它的工作方式就像一个有各种争论的魅力。我认为它可以帮助您轻松解决问题:

.h 档案

- (NSMutableArray *)arrayWithDictionaries:(NSDictionary *)dictionary, ... NS_REQUIRES_NIL_TERMINATION;

.m 档案

- (NSMutableArray *)arrayWithDictionaries:(NSDictionary *)dictionary, ... {
    NSMutableArray *_array = [[NSMutableArray alloc] init];

    // I'm building the array of the arguments
    va_list _arguments;
    va_start(_arguments, dictionary);

    for (NSDictionary *_currentArgument = dictionary; _currentArgument != nil; _currentArgument = va_arg(_arguments, NSDictionary*)) {
        [_array addObject:_currentArgument];
    }

    va_end(_arguments);

    return _array;
}

更新#1 (在090413上)

这是你的代码......

.h 档案

- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

.m 档案

- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [self init];
    if (self)
    {
        // Build normal buttons
        va_list _arguments;
        va_start(_arguments, otherButtonTitles);

        for (NSString *_currentArgument = otherButtonTitles; _currentArgument != nil; _currentArgument = va_arg(_arguments, NSString*)) {
             UIButton *button = [self buildButtonWithTitle:_currentArgument];
             [self.buttons addObject:button];
        }

        va_end(_arguments);

        // Build cancel button
        UIButton *cancelButton = [self buildCancelButtonWithTitle:cancelButtonTitle];
        [self.buttons insertObject:cancelButton atIndex:0];

        // Add primary button
        if (primaryButtonTitle)
        {
            UIButton *primaryButton = [self buildPrimaryButtonWithTitle:primaryButtonTitle];
            [self.buttons addObject:primaryButton];
        }

        // Add destroy button
        if (destructiveButtonTitle)
        {
            UIButton *destroyButton = [self buildDestroyButtonWithTitle:destructiveButtonTitle];
            [self.buttons insertObject:destroyButton atIndex:1];
        }
    }

    return self;
}

更新#2 (在090413上)

你应该这样调用这个方法:

[[... alloc] initWithCancelButtonTitle:@"Cancel Title" primaryButtonTitle:@"Primary Title" destructiveButtonTitle:@"Destructive Title" otherButtonTitles:@"Other Title 1", @"Other Title 2", @"Other Title 3", nil];