Objective-C 所有 UI 实例为零 [英] Objective-C all instances for UI is nil

查看:19
本文介绍了Objective-C 所有 UI 实例为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Objective-C/Xcode for Mac 来做我的第一个项目.

I'm trying to do my first project in Objective-C/Xcode for Mac.

到目前为止,我刚刚从 GitHub 复制了一个示例(https://github.com/phae-girl/multi-view-controllers-xibs).这是一个如何在主窗口中有 2 个子视图的示例.该示例效果很好,但是当我在其中一个视图中添加一个按钮时,新按钮的- (IBAction)newButton:(id)sender{}"方法将两个子视图都显示为nil".我相信这是访问问题,因为文本字段在切换视图之间保持其价值.

So far, I have just copied an example from GitHub (https://github.com/phae-girl/multi-view-controllers-xibs). It is an example of how to have 2 subviews inside a main window. The example works great, but when I add a button inside one of the views, the "- (IBAction)newButton:(id)sender{}" method for the new button shows both subviews as "nil". I'm confident it is a problem with access as textfields keep their value between switching views.

我已经搜索了几个小时,所以感谢任何帮助!

I have been searching for hours, so any kind of help is appreciated!

附言.
以 GitHub 项目为基础重现错误:
1. 拖放按钮到子视图中
2. 在 AppDelegate
的视图中添加 NSObject3. ctrl 拖动 AppDelegate.h
中的按钮4.在AppDelegate.m中的IBAction调用中设置断点

Ps.
To reproduce error with the GitHub project as base:
1. Drag'n'drop button into a subview
2. Add NSObject inside the view for the AppDelegate
3. ctrl-drag the button inside AppDelegate.h
4. Set breakpoint inside IBAction call in AppDelegate.m

推荐答案

在视图中为 App Delegate 添加 NSObject"不是您想要做的.如果您这样做并将类设置为项目的 AppDelegate 类,您就创建了 AppDelegate 的第二个实例,它不是 [NSApp delegate].与 [NSApp delegate] 不同的是,您的新实例没有将其视图控制器属性初始化为 MainMenu.xib 中的出口

"Add NSObject inside the view for the App Delegate" is not what you want to do. If you do that and set the class to the project's AppDelegate class, you just created a second instance of AppDelegate that is not [NSApp delegate]. And unlike [NSApp delegate], your new instance doesn't have its view controller properties initialized to the outlets from MainMenu.xib

在这里查看我的奇妙问题更多讨论.相信我,我感受到你的痛苦.如果您在 IBAction 中执行此操作,则应确认单独的实例地址:

See my fantastic question here for more discussion. Trust me, I feel your pain. If you do this in the IBAction, it should confirm separate instance addresses:

NSLog(@"This instance address: %p",self);
NSLog(@"True app delegate address: %p",[NSApp delegate]);

要理解的关键是NIB 中的对象是在加载NIB 时创建的,而IBOutlets 是指向这些对象的指针.很容易将 NIB 对象视为指向代码中对象的指针,但事实恰恰相反.在这方面,占位符很容易让人误入歧途.

The key thing to understand is that the objects in the NIB are created when the NIB is loaded, and IBOutlets are pointers to those objects. It's all too easy to think of the NIB objects as pointers to objects in your code, but it's truly the opposite. The placeholders make it easy to be lead astray in this regard.

无论如何,要解决您的问题...

Anyways, to fix your issue...

您必须子类化 NSViewController 并使子视图 xib 中文件所有者的自定义类成为您的新子类,并确保 MainMenu.xib 中正确的视图控制器实例是您的新子类.然后,您可以将子视图中的 IBAction 连接到自定义类头,Xcode 将识别新子类和子视图 NIB 之间的关联,因为您已经设置了 File's Owner 的类.我相信您将根据您链接到的项目中的工作方式连接到正确的实例,但如果没有,则有 这个问题要考虑.

You have to subclass NSViewController and make the custom class of File's Owner in the subview xib your new subclass, and also make sure the correct view controller instance in MainMenu.xib is your new subclass. And then, you can connect the IBAction from the subview to the custom class header, and Xcode will recognize the association between the new subclass and the subview NIB because you've set the class of File's Owner. I believe you'll then be hooked up to the right instances based on the way things are done in the project you linked to, but if not, there's this question to consider.

没有好的方法让子视图 NIB 调用 AppDelegate 类中的方法.最好将其视为仅用于主菜单(退出、保存、撤消等)而不用于任何视图.(需要注意的是 很棒问题" 展示了一种丑陋的方法来使它工作.)

There's no good way have the subview NIBs call methods in the AppDelegate class. Best to think of it as there only for the main menu (Quit, Save, Undo, etc) and not for any views. (With the caveat that the "fantastic question" shows an ugly way to make it work.)

您可以将自定义 NSViewController 子类中的代码引用回 [NSApp delegate] -- [[NSApplication sharedApplication] delegate] 的简写 -- 如果需要,虽然你最好避免这种情况.

You could have the code in your custom NSViewController subclass refer back to [NSApp delegate] -- shorthand for [[NSApplication sharedApplication] delegate] -- if needed, though you are best to avoid that.

这篇关于Objective-C 所有 UI 实例为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆