-4

我已经尝试了以下代码,但只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;
}

如何修改它?

4

1 回答 1

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];
于 2013-04-09T11:16:33.280 回答