Variable IBOutlet name for a UILabel?

前端 未结 1 637
死守一世寂寞
死守一世寂寞 2021-01-16 23:59

Ugh, having some trouble here...

I have 4 display labels in Interface Builder set up as IBOutlets in the view controller.

I\'ve set them up with these names

相关标签:
1条回答
  • 2021-01-17 00:54

    You have the right idea, but you should pass valueForKey: to self, not UILabel, assuming its self that owns these properties.

    myLabel = [self valueForKey:[NSString stringWithFormat:@"label%d", randomInt]];
    

    As a side note, this is probably better done with an IBOutletCollection. In Xcode 4, select all of the labels and drag them to the header file. This will generate an IBOutletCollection NSArray containing all the outlets. You can then pick a random index from that array. It is usually unfortunate that IBOutletCollection stores its outlets in a non-deterministic order, but for your case it should be fine.

    0 讨论(0)
提交回复
热议问题