以编程方式创建的视图和界面生成器占位符之间的对象混合

发布于 2025-01-04 00:55:52 字数 558 浏览 4 评论 0原文

我有一个包含滚动视图的视图控制器。滚动视图内部还有另一个 UI 核心图形视图。在视图控制器中,我为核心图形视图创建一个临时对象,并向其分配一些数据,然后将其分配给视图控制器的属性。例如:在视图控制器中:

@interface controller : UIViewController {
        GraphView *graph;
}
@property ... IBOutlet GraphView *graph;

@implementation
GraphView *temp = [[GraphView alloc] init];
temp.someArray = anExistingDataArray;
self.graph = temp;

在IB中,我打开视图控制器笔尖并添加滚动视图,然后嵌入视图并将其分配给核心图形视图类。然后将该视图中的 IBOutlet 连接到视图控制器中的属性。

我的问题是视图控制器创建临时视图对象,使用正确的数据将其分配给自身,但是IB似乎实例化了自己的对象(不同的内存ID)并显示该对象,而不是视图控制器中的对象。

构建此类设置的正确方法是什么?

I have a view controller which contains a scroll view. Inside the scroll view there is another UI core graphics view. In the view controller I create a temp object for the core graphics view and assign some data to it, then assign it to the attribute of the view controller. Eg: In the view controller:

@interface controller : UIViewController {
        GraphView *graph;
}
@property ... IBOutlet GraphView *graph;

@implementation
GraphView *temp = [[GraphView alloc] init];
temp.someArray = anExistingDataArray;
self.graph = temp;

In IB, I open the view controller nib and add a scroll view, and embed a view and assign it the core graphics view class. Then hook up the IBOutlet from that view to the attribute in the view controller.

My problem is that the view controller creates the temp view object, assigns it to itself, with the correct data, however IB seems to instantiate its own object (different memory ID) and displays that, instead of the one in the view controller.

What is the correct way to build this type of setup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2025-01-11 00:55:52

如果将对象拖动到笔尖中,IB 会创建并归档该对象。这就是为什么您不必分配/初始化在 IB 中创建的视图。

我猜您正在 IB 中创建视图,以便获得正确的几何图形,或者...?创建视图然后在运行时立即替换它是相当不寻常的。出于几何目的,更常见的是在 IB 中创建容器视图,然后添加以编程方式创建的视图作为其子视图。

不过,您的代码遗漏了其中最重要的部分,即它运行时的情况。是在 -init... 中吗? -awakeFromNib? -加载视图? -viewDidLoad?确切的位置很重要,因为它们以明确的顺序发生。如果您将代码放在错误的位置,它将在笔尖取消存档并完全重新连接之前运行,因此笔尖将破坏您的代码执行的任何操作。

那么:你的 [self setGraph] (我不能让自己使用点语法)代码什么时候运行?

If you drag an object into a nib, IB creates and archives that object. This is why you don't have to alloc/init views that you create in IB.

I'm guessing that you are creating your view in IB so that you can get the geometry correct, or...? It's rather unusual to create a view and then immediately replace it at run-time. More common, for geometric purposes, is to create a container view in IB and then add your programmatically-created views as subviews of that.

Your code left out the most important piece of this, though, which is when it's getting run. Is it in -init...? -awakeFromNib? -loadView? -viewDidLoad? The exact location matters since these occur in a well-defined sequence. If you put your code in the wrong place, it will run before the nib is unarchived and fully reconnected, so the nib will clobber whatever your code did.

So: when is your [self setGraph] (I can't bring myself to use dot syntax) code getting run?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文