5

I'm sure this must be answered somewhere already but I'm struggling to find the right search terms for the answer.

In my objective-c code I have an NSArray of an unknown number strings that I want to pass its elements to a variadic init method, in this case its the ... list of 'otherButtonTitles' in the constructer of UIActionSheet. How can this be achieved?

thanks in advance

0

2 Answers 2

3

I think you would need to pass the first element of the array to the constructor and then use the addButtonWithTitle method to loop through the remaining elements and add them:

UIActionSheet *mySheet = [[UIActionSheet alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:[myOtherButtons objectAtIndex:0],nil];

NSMutableArray *otherbuttons = myOtherButtons;
[otherButtons removeObjectAtIndex:0];

NSEnumerator *enumerator = [otherButtons objectEnumerator];
id anObject;

while (title = [enumerator nextObject]) {
    [mySheet addButtonWithTitle:title];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I guess I was expecting something slightly more elegant, thanks though, works fine.
3

There isn't a general way to do this, but for UIActionSheet specifically, you don't need to use that constructor.

UIActionSheet *sheet = [[UIActionSheet alloc] init];

// set properties
sheet.title = @"Title!";

// add buttons
for (NSString *buttonTitle in otherButtonTitles) {
    [sheet addButtonWithTitle:buttonTitle];
}

sheet.cancelButtonIndex =
    [sheet addButtonWithTitle:@"Cancel"];

UIAlertView can be initialized in a similar way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.