将同一个 NIB 与多个视图控制器一起使用

发布于 2024-10-22 05:49:46 字数 264 浏览 5 评论 0原文

基本上,我想使用 nib 文件和视图控制器作为我计划多次创建的视图的模板。该笔尖将有几个标签和自定义视图。 我的想法是,我将迭代一个对象数组,并为每个对象创建一个该控制器的实例,并将一个属性设置为数组中对象的属性。

目前这一切都运行良好,除了一件事 - 当我调用 setStringValue: 时标签不会更新!

我正在视图控制器的代码中使用一种方法来进行更改,但它不起作用,我猜测 IBOutlet 没有正确连接,这很奇怪,因为自定义视图完美连接。

有什么想法吗?

Basically I want to use a nib file and view controller as a template for a view that I plan to create a number of times. This nib will have a couple of labels and custom views.
The idea is that I will iterate through an array of objects and for each I will create an instance of this controller and set a property to that of the object from the array.

This all works nicely at the moment except for one thing - the labels won't update when I call setStringValue: !!!

I'm using a method within the view controller's code to make the change but it just doesn't work, I'm guessing that the IBOutlet isn't being hooked up properly, which is strange because the custom views are hooking up perfectly.

Any ideas?

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

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

发布评论

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

评论(3

李白 2024-10-29 05:49:46

在 awakeFromNib 上设置断点,然后在调试器中查看标签出口的值是什么。在调用 awakeFromNib 之前,所有插座都应该已连接。如果它仍然为零,你就有答案了。在 nil 上调用 setStringValue: 完全“什么也不做”。在这种情况下,您没有正确绑定插座,或者也许您曾经正确绑定过它,后来更改了名称,在这种情况下,Xcode4或界面构建器中应该有一个黄色警告三角形,表明出现了问题;但是,它不会阻止您的应用程序构建或运行,插座将在对象创建后简单地保留其初始值(为零)。

Set a breakpoint on awakeFromNib and look in the debugger what the value of the label outlet is. All outlets should have been connected before awakeFromNib is being called. If it is still nil, you have your answer. Calling setStringValue: on nil does exactly "nothing". In that case you have not correctly bound the outlet or maybe you once had it bound correctly and later on changed the name, in that case there should be a yellow warning triangle in Xcode4 or interface builder indicating that something is wrong; however it will not prevent your app from building or running, the outlet will simply keep its initial value after object creation (which is nil).

辞旧 2024-10-29 05:49:46

当您分配 NSViewControllers 时,只需使用 NIB 名称进行初始化:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

When you alloc your NSViewControllers, just init with name of NIB:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
森林迷了鹿 2024-10-29 05:49:46

感谢您的回复,它们很有帮助,但不完全符合我的意思。
我最终通过创建一个空的 NIB 并用自定义 NSView 和一些其他控件填充它来解决这个问题。我使用 IBOutlets 为这些控件创建了一个 NSView 子类,并将自定义视图的标识设置为界面生成器中的子类。
每次我想要绘制它时让它工作的技巧是在我的子类中创建一个类方法,该方法将加载笔尖并返回按我想要的方式设置的视图。

代码如下:

+(id)todoViewFromNibWithFrame:(NSRect)frameRect todoList:(TodoList *)aTodoList
{
    NSNib *todoViewNib = [[NSNib alloc] initWithNibNamed:@"TodoView" bundle:nil];
    NSArray *objects = nil;

    id todoView = nil;

    [todoViewNib instantiateNibWithOwner:nil topLevelObjects:&objects];
    for (id object in objects) {
        if ([object isKindOfClass:[self class]]) {
            todoView = object;
            [todoView setTodoList:aTodoList];
            break;
        }
    }

    [todoViewNib release];
    return todoView;
}

再次感谢您的回复!
史蒂夫

Thanks for the replies, they were helpful but not quite what I was getting at.
I ended up solving it by creating an empty NIB and filling it with just a custom NSView and a few other controls. I created an NSView subclass with IBOutlets for those controls and set the custom view's identity to my subclass in interface builder.
The trick in getting it to work each time I wanted to draw it was by making a class method in my subclass that would load the nib and return the view set up the way I wanted.

Code below:

+(id)todoViewFromNibWithFrame:(NSRect)frameRect todoList:(TodoList *)aTodoList
{
    NSNib *todoViewNib = [[NSNib alloc] initWithNibNamed:@"TodoView" bundle:nil];
    NSArray *objects = nil;

    id todoView = nil;

    [todoViewNib instantiateNibWithOwner:nil topLevelObjects:&objects];
    for (id object in objects) {
        if ([object isKindOfClass:[self class]]) {
            todoView = object;
            [todoView setTodoList:aTodoList];
            break;
        }
    }

    [todoViewNib release];
    return todoView;
}

Thanks again for the replies!
Steve

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