开发者

How do I dynamically create an NSString object based on the number of objects in an array?

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 dele开发者_运维技巧gate 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!


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];


I will prefer using an array of strings.


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].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜