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!