1

I'm looking at this question and wondering how it was known or where it is documented that [[segmentedControl subviews] objectAtIndex:0] is the currently selected control segment?

That is - how do we know which objectAtIndex number to use?

Community
  • 1
  • 1
johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • No where it is mention that [[segmentedControl subviews] objectAtIndex:0] will only be the selected segment.Based on property value isSelected which returns BOOL value it is decided which segment is selected.for (int i=0; i<[sender.subviews count]; i++) { if ([[sender.subviews objectAtIndex:i]isSelected] ) { UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0]; [[sender.subviews objectAtIndex:i] setTintColor:tintcolor]; break; } } – Nuzhat Zari Jun 01 '12 at 08:26

2 Answers2

2

If you look at other responses, you see that they look for the one that is selected, that is, check what isSelected returns

for (int i = 0; i < [sender.subviews count]; i++) 
{
  if ([[sender.subviews objectAtIndex:i]isSelected]) 
  {               
    ...
  }
}
Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
0

I have to say that the code Frowing mentioned may crash. class checking is needed.

        for (int i = 0; i < [sender.subviews count]; i++) {
         UIView *seg = [sender.subviews objectAtIndex:i];
             if ([seg isKindOfClass:[UISegmentedControl class]]&&[seg isSelected]) 
             {               
             ...
             }
         }
Henry
  • 481
  • 4
  • 17