2

I am currently using unified storyboards (with size classes). For 1 of my views, the difference in design between the wAny hAny and the wRegular hRegular size classes is significant enough that altering the constraints for the subviews based on size class is not enough.

A simplified example: I have a UILabel in the wAny hAny size class called "First_Name_Label". While I wish to reuse this UILabel for the wRegular hRegular size class, the design is too different, so I add a UILabel for the wRegular hRegular size class to replicate the purpose of the "First_Name_Label", and I call it "First_Name_Label 2". The "First_Name_Label" is only installed in the wAny hAny size class, and "First_Name_Label 2" is only installed in the wRegular hRegular size class.

In my code, I want to set the text for the label:

self.First_Name_Label.text = "my first name"

But I need to do it for the other size class as well, and my code would be as follows:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomIpad) {
    self.First_Name_Label2.text = "my first name"
} else {
    self.First_Name_Label.text = "my first name"
}

This seems very cumbersome, basically doubling my current code. I'm sure there's a more elegant and efficient way to reference these 2 IBOutlets (each belonging to a different size class) at once.

mrl
  • 1,467
  • 2
  • 14
  • 22

3 Answers3

2

You can't connect an IBOutlet property with more than one object.

But you can use the same tag on those labels and get to them like this:

(UILabel *)[self.view viewWithTag:LABEL_TAG];
Aura
  • 246
  • 1
  • 10
  • In this case, I wouldn't have to create another IBOutlet (just add a tag to the other UILabel), but would viewWithTag only return 1 UILabel? So I would still have to write separate code for the other size class's UILabel anyway? – mrl Jun 10 '15 at 17:09
  • 1
    No, you don't have to write separate code because only one label will be available at a given size class. – Aura Jun 11 '15 at 07:31
1

Just use optionals:

self.First_Name_Label2?.text = "my first name"
self.First_Name_Label?.text = "my first name"

In this case First_Name_Label and First_Name_Label2 are optional and if it is not initialized the code will just ignore it. Make use they are declare as optionals in the class as well:

var First_Name_Label:UILabelView?
var First_Name_Label2:UILabelView?
Icaro
  • 14,585
  • 6
  • 60
  • 75
  • Thanks Icaro. My problem with this would be that is would still require me to write out another line of code every time I want to do something with the label (1 line for each label), although I would have to use the If/else with this. – mrl Jun 10 '15 at 17:03
1

Have you consider using an IBOutletCollection?

e.g.

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

In code, you call:

for (UILabel *First_Name_Label in self.First_Name_Labels) { First_Name_Label.text = "my first name"; }

This also easily allows you to expand your scope to more size classes.

Toland Hon
  • 4,549
  • 2
  • 32
  • 36