objective-c - 如何使用单独的 viewController 正确设置 NSPopover

标签 objective-c cocoa interface-builder nspopover

我创建了一个非常简单的测试程序。它有一个按钮。单击该按钮会弹出一个带有标签的 NSPopover。该标签位于一个单独的 xib 文件中,具有自己的 NSViewController

目标是,当我在主屏幕上单击按钮时,弹出窗口将显示viewcontroller的xib文件。 xib 文件的标签应将其文本设置为“It Works”。

嗯..它有效,但仅在弹出窗口第二次加载之后。第一次单击按钮时,标签仍具有旧的默认值。但从第二次点击开始,“它有效”。 有人知道是什么原因导致了这个问题吗?它只有大约5行代码代码可以在这个存储库中看到 --> https://github.com/patchthecode/testtest

最佳答案

- (void)windowDidLoad方法中调用[mainScreenPopoverViewController view];。这会将您的 View 加载到内存中。

调用之前[mainScreenPopoverViewController view];(文本字段地址为0x0)

enter image description here

您不应该对所有 IBOutlet 使用强属性。

@property (nonatomic, strong) IBOutlet NSTextField *textField;

看看Resource Programming Guide

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/

相关文章:

ios - 在 iOS 中创建和部署静态库时避免 header 冲突

objective-c - "objectAtIndex"的 NSUserDefaults 错误

objective-c - '-(bool)validateToolbarItem:(NSToolbarItem *)theItem' 的类型冲突

ios - 如何在 IB 中设置 UISplitViewController 委托(delegate)?

ios - 自定义字体在界面生成器 Xcode 6 中一段时间​​后消失

ios - 如何让 UILabel 在单词末尾显示 "..."而不是截断它?

ios - 根据视频的宽高比调整 AVPlayerItem 容器 View 的大小

objective-c - 比较系统时间和NSDatePicker时间

swift - 使用 Swift : "Any" printer setup has irregular margins/page size, cocoa 打印有解决方法吗?

ios - 无法在iOS应用程序中使用AFNetworking