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

标签 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];
}

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

最佳答案

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

关于ios - 通过另一个类以编程方式将对象添加到 xib 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28290716/

上一篇:ios - UILabel 看到最右边的内容

下一篇:iOS8 CoreMotion 在应用暂停时从 startActivityUpdatesToQueue 获取更新

相关文章:

objective-c - UIButton 滑动而不是点击

ios - 哪个控件用于其中一个或值?

iPhone 模拟器在应用程序启动时崩溃

xcode - -Wformat=2 有什么作用?

ios - 使用 iBeacon 触发音频

iOS 在形状中心绘制文本

ios - UIScrollView 为 self.view 添加缩放

ios - 按 BPM 将音轨分成多个片段,并使用 Superpowered iOS 分析每个片段

android - 在 FCM 中添加 collapse_key

ios - PopoverPresentationController不会在触摸外部时被解雇