Understanding NSPopover with ARC



我对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中控件的数据源和委托,但一般来说,数据源和委托不会被保留,对吧?

我意识到你的问题现在有点老了,但我在研究我的NSViewController和NSPopover的保留周期时遇到了它:

NSPopover contentViewController属性保留你的NSViewController。这就是为什么您可以像您(和我)一样显示弹出窗口作为对操作的响应,而不需要另一个对象保留它。我发现,虽然,是正确释放NSViewController在ARC下,contentViewController应设置为nil时弹出窗口关闭。这在我的NSViewController子类中:

- (void)popoverDidClose:(NSNotification *)notification
{
    self.popover.contentViewController = nil;
}

最新更新