代码之家  ›  专栏  ›  技术社区  ›  emenegro

填充uilabel文本而不链接iboutlet

  •  2
  • emenegro  · 技术社区  · 15 年前

    我想在用ib创建的不同ui视图上用相同的文本填充几个ui标签。

    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.

    我可以这样做吗?也许只有一种语言的本地化(ibtool)?

    3 回复  |  直到 14 年前
        1
  •  4
  •   Cheddar    15 年前

    我想您可以将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).

        2
  •  1
  •   Girish Kolari    14 年前

    其中一种方法可以这样:

    1. 在ib中创建所有的ui组件。
    2. 为其中的每个控件定义适当的(唯一的)标记。
    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. 编写一个逻辑来管理具有您检索的特定标记的所有控件。
    5. 执行相应的控制操作。

    即使你有不同的笔尖或视图,重复这个方法来获得细节

        3
  •  0
  •   xmr    14 年前

    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;
            }
        }
    }