0
votes

I create a custom view which has exactly same function as UIAlertView. It also has instance method same as UIAlertView. I take a look at documentation, here is the method declareation "- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ..."

So I want to know in the method implementation, how do I know how many buttons are returned and get their titles in my custom view.

Thank you.

1

1 Answers

2
votes

You get the additional titles via a va_list. The parameter otherButtonTitles will be the first object in the list, and you can use va_arg() to iterate through the list until you encounter nil, which terminates the list.

Here's the code:

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    self = [super init];

    if (self)
    {
        va_list args;
        va_start(args, otherButtonTitles);

        NSMutableArray *buttonTitlesArray = [NSMutableArray new];

        while (otherButtonTitles != nil)
        {
            [_buttonTitles addObject:otherButtonTitles];
            otherButtonTitles = va_arg(args, NSString *);
        }

        // otherButtonTitles now contains all of your button titles
        // Finish configuration of your view here
    }
    return self;
}