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.

3

There are 3 answers

2
Aura On BEST ANSWER

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];
1
Icaro On

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?
0
Toland Hon On

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.