views:

36

answers:

3

Members of my favorite forum, I am stuck with a simple problem (chalk it up to the fact that I am fairly new)...

I am using an action sheet. I don't know how many buttons to display, so I decided to set the action sheet delegate to display FOUR buttons in total using string variables, knowing that if I only need to display 2, I can set the third variable to NIL and achieve that objective.

Example:

NSString *firstVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:0]];
            NSString *secondVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:1]];
            NSString *thirdVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:2]];
            NSString *fourthVehicle = [[NSString alloc] initWithString:[myVehicleList objectAtIndex:3]];

            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please select a vehicle:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:firstVehicle, secondVehicle, thirdVehicle, fourthVehicle, nil];
            NSLog(@"firstOtherButtonIndex in the List is %i", [actionSheet firstOtherButtonIndex]);
            [actionSheet showInView:self.view];
            [actionSheet release];
            break;

With that said, I don't always know that i will have four strings to create...when I pull back the myVehicleList array, I can use the count method to determine how many variables I have.

Question: So how does a smart programmer actually solve this problem? Meaning, how do I cycle through a for statement perhaps using the count returned when I check the array to only establish a certain number of strings and set the next string to nil?

Does that make sense? I can do IF statements for days, right. If count = 4, then set all four strings. But if count is 3, I set three strings to the array object values and the fourth to nil. But if the count is 2, I set two strings to the array object values and the third to nil, and so on.

There has to be a more effective way to dynamically create strings? I guess I could use fast enumeration, but how do I dynamically create a unique string object...

I hope I make some sense.

thanks!

A: 

I will prefer using an array of strings.

taskinoor
A: 

The real problem you're up against is that there's no good way to convert between a dynamic array and a varargs parameter (that is, one where you can pass any number of variables as arguments). If you wanted to do it the looping way and still use the varargs parameter, the best approach would be to create an array like NSString *strings[4] = {nil}, loop through it and then pass strings[0], strings[1], strings[2], strings[3] as arguments.

But I wouldn't do that. It's really duct-tapey. I would create the sheet with only the cancel button, then loop through an NSArray of strings that you want to use as titles and [sheet addButtonWithTitle:title].

Chuck
A: 
NSMutableArray *vehicles = [[NSMutableArray alloc] init];

[vehicles addObject:@"One"];
[vehicles addObject:@"Two"];
[vehicles addObject:@"Three"];
[vehicles addObject:@"..."];
[vehicles addObject:@"Ten"];

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please select a vehicle:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *vehicle in vehicles) {
    [actionSheet addButtonWithTitle:vehicle];
}   
[actionSheet showInView:self.view];
[actionSheet release];
ohho
Thank you, everyone. This site is excellent thanks to those of you who contribute. The above code and also the responses where folks think out loud and explain options really helps.
newDeveloper