2

I have an IBoutletCollection property connected to several UILabels in Storyboard:

@property (weak, nonatomic) IBOutletCollection(UILabel) NSArray *labels;

called

NSLog(@"%ld",(long)self.labels.count) after viewDidLoad

got 0 as result.

what's the problem I got empty ivar here?

anything wrong with the Autolayout? (Yes I'm using it in Storyboard)

Thanks for any advice!

bluenowhere
  • 2,683
  • 5
  • 24
  • 37
  • 1
    Make labels strong reference. – Desdenova Aug 26 '15 at 08:09
  • 1
    If they are not strong, the reference will be lost immediately after they had been loaded – Alex Cio Aug 26 '15 at 08:13
  • 1
    Thanks @亚历山大 ! but how come an auto-generated IBoutlet property (dragged from Storyboard to class interface) has weak attribute? – bluenowhere Aug 26 '15 at 08:20
  • 1
    And if I do this with an xib the weak attribute does not matter ... – bluenowhere Aug 26 '15 at 08:21
  • 1
    Normally you connect IBOutlets with a weak reference and everything works fine. But if you connect an IBOutletCollection it is a type of NSArray which needs a strong reference to save its elements – Alex Cio Aug 26 '15 at 09:04

1 Answers1

1

Change weak to strong

@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *labels;

tuledev
  • 10,177
  • 4
  • 29
  • 49