Firemarble Firemarble - 5 months ago 10
iOS Question

Setting initial selected index based up view's tag

I am trying to set the selected index of a UISegementControl based up on a views tag.

I have a special list of tags, that might change over time.

I run through a for loop to set the view's tag.

[[[self.segmentDuty subviews] objectAtIndex:i] setTag:someListofTags[i]];


That works fine as I can press the segment and get the correct tag.

NSInteger tagnumber = [[self.segmentDuty.subviews objectAtIndex:[sender selectedSegmentIndex]] tag];


The problem I having is setting the initial SELECTED index.

for (UIView * view in self.segmentDuty.subviews){
if(view.tag == someInitialTagNumber){

??? HERE is where I am having issue making the connection between index and tag
//[self.segmentDuty isSelected];

break;
}
}


Any help?

Answer

I don't see where you set the selectedIndex, how about do it this way.

for (int i = 0; i < [self.segmentDuty.subviews count]; i++) {
    UIView* view = [self.segmentDuty.subviews objectAtIndex: i];
    if(view.tag == someInitialTagNumber){

            self.segmentDuty. selectedSegmentIndex = i;
            break;
    }
}