0

I'm trying to add 2 segments to an array, then loop through its subviews. Here is my code:

NSArray *myArray = [[NSArray alloc] initWithObjects:[self.segment.subviews objectAtIndex:0],
                                   [self.dataSegmentedControl.subviews objectAtIndex:1], nil];


for (id innerView in myArray) // I tried "myArray.subviews" but it didn't let me do ".subviews"
{
    if ([innerView isKindOfClass:[UILabel class]]) {
        ...
    }
}

When I insert an NSLog in the if statement, I don't get any output. What can I do to fix that?:

Jessica
  • 9,379
  • 14
  • 65
  • 136
  • 1
    What is the value of `[self.segment.subviews objectAtIndex:0]` and `[self.dataSegmentedControl.subviews objectAtIndex:1]`? Maybe neither is a `UILabel`? @Jessica – tpatiern May 22 '15 at 00:34
  • I think their subviews are – Jessica May 22 '15 at 00:38
  • The following works: `for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews])` But I need to do it on only 2 segments. – Jessica May 22 '15 at 00:39
  • 2
    What are you trying to accomplish? – Duncan C May 22 '15 at 00:41
  • I'm trying to change the font size of 2 segments. – Jessica May 22 '15 at 00:45
  • 1
    Keep in mind that attempting to dig into the private subview structure of a standard control is only going to result in bugs and crashes every time there is an iOS update and the structure changes. If a control doesn't provide the customization you need through the provided API, the proper solution is to write or find a custom control that does what you need. – rmaddy May 22 '15 at 01:13
  • BTW - this seems like a duplicate of [your previous question](http://stackoverflow.com/questions/30364868/how-to-change-the-font-size-of-a-single-index-for-uisegmentedcontrol). – rmaddy May 22 '15 at 01:15
  • I wasn't sure whether I should edit that question or add a new question. The reason created a new question, was because I thought since this is on a kind of different topic, (adding segments to an array,) I thought it would be better to create a new question. – Jessica May 22 '15 at 02:20
  • So are you saying I shouldn't even do what this answer says? http://stackoverflow.com/questions/30364868/how-to-change-the-font-size-of-a-single-index-for-uisegmentedcontrol#answer-30365179 – Jessica May 22 '15 at 02:22

1 Answers1

0

There are only two possible reasons for what you see:

  1. None of the array's elements is a UILabel
  2. There are no elements in the array. This could be because [self.segment.subviews objectAtIndex:0] returns nil, which only means that the self.segment is nil.

Try to explore the view hierarchy, before you assume anything. So print out the segments and their subviews to know the hierarchy.

However, if you want to change the font size of some segments but not the others, I'd recommend not to go this way and mess with the internal structure. If it worked out, you should expect a problem with any upcoming system update. I suggest you search for other 3rd party components or build your own.

Mohannad A. Hassan
  • 1,650
  • 1
  • 13
  • 24