I have a UISegmentedControl with several segments, each with a different "title". I want to be able to read in a NSString, and programmatically select the segment whose title matches that string. Say I start with something like:
NSString *stringToMatch = @"foo";
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"foo",@"bar",@"baz", nil]];
I want to do something like:
[seg selectSegmentWithTitle:stringToMatch];
But since there is no method called selectSegmentWithTitle, this doesn't work. Is anyone aware of a method that would be similar to this though?
I've also thought of looping over all the titles in seg, similar to this:
int i = 0;
for (UISegment *thisSeg in [seg allSegmentsInOrder])
{
if ([thisSeg.title isEqualToString:stringToMatch])
{
[seg setSelectedSegmentIndex:i];
break;
}
i++;
}
but to my knowledge there is no such thing as UISegment, nor is there a method allSegmentsInOrder. Again, does anyone know of any changes I could make to get this to work?
Thirdly, I could probably subclass UISegmentedControl to somehow add the methods I want it to have. I hate subclassing like that though, cause I'd have to go and re-declare all my segments and other inconvenient things like that. But it may be the only way to go...
Perhaps the way to do this is totally different from the three ideas I listed above. I'm open to whatever.