以编程方式通过另一个类将对象添加到xib文件

时间:2015-02-03 03:12:00

标签: ios objective-c xcode xib

我有一个.m和.h文件链接到一个名为cusomxib的xib。在xib中我添加了一个UIView。

如何以编程方式将标签添加到myVIew(从xib文件)到另一个类?如下所示,它无法创建xib文件的新实例,因为它将创建一个新实例。我想访问原始版本。

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
    [self.label setText:@"This is a label"];

    xibView *myxibView = [[xibView alloc] init]; // This is the part thats wrong. I need to use something that just gets the file, not create a new one.
    [myxibView.myView addSubview:self.label];
}

希望这很清楚。如果你需要我澄清一些事情,我很乐意。

1 个答案:

答案 0 :(得分:1)

- (void)viewDidLoad {
    [super viewDidLoad];

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
    [self.label setText:@"This is a label"];
    [self.view addSubview:self.label];
}