Connect two labels to one outlet

2019-09-02 13:59发布

Now I understand that this question has been asked before, but the answers were unsatisfactory. My issue is that I have a view controller with a view and stuff in it including a label. I added a bunch of code for it and now I'm expanding on it. I now have an issue where I've decided to add another UIView to my interface and it has a label and that label is going to function EXACTLY like a label I have in my first UIView. My problem is that I don't want to have to go in my view controller method and add another line of code each time I manipulate that first label. Is there anyway I can link another label to my initial IBOutlet I have set for my first label? Or do I have to go in my code and add an extra line of code everytime I manipulate that first label?

3条回答
ら.Afraid
2楼-- · 2019-09-02 14:09

It depends on what you want to do to that label. If you're looking to change some of the attributes of the label in the same way (e.g., font, text colour, alignment) then you can put both labels in an IBOutletCollection and iterate over the collection in your view controller.

If you want to have different data in the label, but other attributes the same, then you'll need a separate IBOutlet for that label.

You can combine the two techniques as well. e.g.

(interface)

@property (weak, nonatomic) IBOutlet UILabel *firstName;
@property (weak, nonatomic) IBOutlet UILabel *lastName;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *labels;

(implementation)

- (void)viewDidLoad {
  [super viewDidLoad];

  for (UILabel *aLabel in self.labels) {
    // Set all label in the outlet collection to have center aligned text.
    [aLabel setTextAlignment = NSTextAlignmentCenter;
  }

  [self.firstName setText:@"First Name"];
  [self.lastName setText:@"Last Name"];
}
查看更多
干净又极端
3楼-- · 2019-09-02 14:12

Swift 3, Xcode 8

  • Create a prototype cell with objects
  • then add another prototype
  • It will copy the objects from the first prototype cell.
  • The new objects will be connected to the same IBOutlet

Also, copy and pasting objects maintains IBActions, but does not maintain IBOutlets.

I hope this answers your question, as none of the other answers had this work around.

查看更多
Lonely孤独者°
4楼-- · 2019-09-02 14:26

Basically the simple answer is no. Whether you use outlets or an outlet collection or tags or whatever, you ultimately have one reference in your code to one label in your interface, and another reference in your code to another reference in your interface. You can compress your mode of expression so as to cycle readily through those references (as suggested in a different answer), but the basic fact is inescapable that, ultimately, the only way to "talk to" a label is through the one reference that points to that label and to that label alone.

The only way of getting around that is not to use direct references at all. For example, a single message can be sent to multiple recipients by using an NSNotification. So you could have two instances of some UILabel subclass of your own, and "shout" to both instances simultaneously by posting a notification from your view controller - the notification is then automatically passed on to both labels, because you have arranged beforehand for them to register for it.

Similarly, another alternative is that you could use key-value observing so that a change in your view controller is automatically propagated to both labels automatically because they "observe" the change, meaning they are sent notifications - really just an inverted form of NSNotification. (If this were Mac OS X, you could make a simpler, safer version of this arrangement by using "bindings".)

However, I really cannot actually recommend that approach. The truth is that we still live in an excruciatingly primitive world of text-based programming, one line at a time, one command at a time, one reference at a time, and we must just bite the bullet and get on with it.

查看更多
登录 后发表回答