我对 ARC 下的对象生命周期有些困惑。我认为这是一个可能很常见的场景。
1) 为了响应某些事件,从 Nib 加载 NSViewController。
- (IBAction) doIt: (id) sender
{
InfoController *editor=[[InfoController alloc]initWithNibName:@"InfoController" bundle:nil];
[editor show: .... ]
}
2) InfoController 然后显示一个 NSPopover。
3) 稍后,用户在 NSPopover 外部单击。弹出窗口自行关闭。
但是InfoController什么时候发布呢?就此而言,在 doIt
返回后是什么让它保持活力?在我的实现中,InfoController 是其 NSPopover 中控件的数据源和委托(delegate),但通常不会保留数据源和委托(delegate),对吗?
最佳答案
我意识到你的问题现在有点老了,但我在使用 NSViewController 和 NSPopover 研究保留周期时遇到了它:
NSPopover contentViewController
属性保留您的 NSViewController。这就是为什么您可以像您(和我)一样显示弹出窗口作为对操作的响应,而无需其他对象保留它。但我发现,要在 ARC 下正确释放 NSViewController,当弹出窗口关闭时,应该将 contentViewController
设置为 nil。这是在我的 NSViewController 子类中:
- (void)popoverDidClose:(NSNotification *)notification
{
self.popover.contentViewController = nil;
}
关于cocoa - 了解 NSPopover 与 ARC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17151632/