简体   繁体   English

将UISegmentedControl中的细分添加到NSArray

[英]Add segments from a UISegmentedControl to an NSArray

I'm trying to add 2 segments to an array , then loop through its subviews. 我试图将2 segments添加到array ,然后遍历其子视图。 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. 当我在if statement插入NSLog时,没有任何输出。 What can I do to fix that?: 我该怎么解决?

There are only two possible reasons for what you see: 您看到的只有两个可能的原因:

  1. None of the array's elements is a UILabel 数组的任何元素都不是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. 这可能是因为[self.segment.subviews objectAtIndex:0]返回nil,这仅意味着self.segment为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. 我建议您搜索其他第三方组件或构建自己的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM