views:

105

answers:

3

I want to fill several UILabels, on different UIViews created with IB, with the same text.

Now I'm doing it connecting each label with an IBOutlet and filling the text programmatically with a constant string defined on a constants file.

What I want is to avoid the connection with an IBOutlet so I can link the desired string token in IB.

Can I do this? Maybe with localization (ibtool) with only one language?

+4  A: 

I suppose you could subclass UILabel:

@interface MyLabel : UILabel {
}

@end

@implementation MyLabel

- (void) awakeFromNib {
    self.text = @"MyText"; // load text from your constants file here
}

@end

and set the class to MyLabel in IB. If you have multiple strings you want to use on multiple labels, you could extend this using IB tags and checking them in awakeFromNib (matching them to a key in your constants file).

John N
It's not exactly what I'm thinking but it's a good approach. Thank you John.
Espuz
+1  A: 

One of the approach can like this:

  1. Create all UI component in the IB.
  2. Define proper(unique) tag for each control you have in it.
  3. In the application go to content View (which is the supper view of all controls) in you window and get all Sub views (array of sub views are controls).
  4. Write a logic to manage all controls with specific tag you retrieve.
  5. Perform respective control operation.

Even if you have different Nib or View repeat this approach to get the detail

Girish Kolari
A: 

Assuming that they are all in the same NIB file, you can iterate through the contents of the NIB looking for UILabel instances and then assign the text to the labels that are present. For finer grained control make use of the Interface Builder Tags and assign a number to the labels that you wish to modify.

NSString *myStr = @"Bob";
NSArray *contents = [[NSBundle mainBundle] loadNibNamed:@"NameOfViewNib" owner:self options:NULL];

for(id nibItem in contents) {
    if ([nibItem isKindOfClass:[UILabel class]]) {
        UILabel *lbl = (UILabel*)nibItem;

        /* Optionally check for a particular tag if you want to filter them */
        if (lbl.tag == 1) {
            lbl.text = myStr;
        }
    }
}
xmr