gpt4 book ai didi

Objective-C:Popover 似乎总是为零

转载 作者:行者123 更新时间:2023-12-03 17:09:08 39 4
gpt4 key购买 nike

我目前正在使用 cocoa 为 SketchApp 编写一个插件。

我尝试在那里使用 NSPopover ,单击按钮时应该由 IBAction 触发。问题是:弹出窗口没有显示,并且在检查应该保存弹出窗口的变量时,它是nil

我在 Interface Builder 中创建了 NSPopover,因此不是在代码中以编程方式创建的;然后在我的链接类头文件中定义了一个 IBOutlet 绑定(bind);最后在我的实现类中使用这个变量。

这是我的源代码:

MyComponent.h

// imports skipped...

@interface

@property (nonatomic, weak) IBOutlet NSTextField *componentDescription;
@property (nonatomic, weak) IBOutlet NSTextField *componentGuid;
@property (nonatomic, weak) IBOutlet NSButton *guidCopyButton;
@property (nonatomic, weak) IBOutlet NSPopover *popover;

-(IBAction)onCopyButton_Clicked:(id)sender;

@end

MyComponent.m

-(IBAction)onCopyButton_Clicked:(id)sender {

// copy stuff to clipboard
// [...]

// show copied popover
[_popover showRelativeToRect:[sender bounds]
ofView:sender
preferredEdge:NSMinYEdge];
}

在我的 xib-view-file 中,我将 NSPopover-Object 链接到 IBOutlet NSPopover *popover;。但是,当在我的类实现中检查 _popover 时,它始终为 nil

最佳答案

好吧,经过一番调整后,我想我终于明白出了什么问题。将内存模式从 @property(nonatomic,weak)NSPopover *popover; 更改为 ...(nonatomic,strong)...; 后,实例不再是 nil 不再了。

我认为与NSButton(例如)的区别在于,该按钮在xib模板中使用,因此按钮的引用不会被清理。然而,Popover 并不直接“使用”,因此在实际使用之前它可能会被清理。因此,将其从 weak 更改为 strong 似乎就是这里的重点。

但是:此调整是否会产生我可能想以某种方式解决的意外/意想不到的副作用?我现在必须自己负责弹出框的销毁吗?

关于Objective-C:Popover 似乎总是为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51561619/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com