我创建了一个非常简单的测试程序。它有一个按钮。单击该按钮会弹出一个带有标签的 NSPopover
。该标签位于一个单独的 xib 文件中,具有自己的 NSViewController
。
目标是,当我在主屏幕上单击按钮时,弹出窗口将显示viewcontroller
的xib文件。 xib 文件的标签应将其文本设置为“It Works”。
嗯..它有效,但仅在弹出窗口第二次加载之后。第一次单击按钮时,标签仍具有旧的默认值。但从第二次点击开始,“它有效”。 有人知道是什么原因导致了这个问题吗?它只有大约5行代码代码可以在这个存储库中看到 --> https://github.com/patchthecode/testtest
最佳答案
在- (void)windowDidLoad
方法中调用[mainScreenPopoverViewController view];
。这会将您的 View 加载到内存中。
调用之前[mainScreenPopoverViewController view];
(文本字段地址为0x0)
您不应该对所有 IBOutlet 使用强属性。
@property (nonatomic, strong) IBOutlet NSTextField *textField;
From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should therefore typically be weak, because:
Outlets that you create to subviews of a view controller’s view or a window controller’s window, for example, are arbitrary references between objects that do not imply ownership.
The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).
关于objective-c - 如何使用单独的 viewController 正确设置 NSPopover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17519383/