如何防止绑定到 self 导致的循环引用
我有一个应用程序,我需要从子视图访问模型数据。我一直在使用绑定来跨视图传递数据;但是,与 self 的绑定似乎会导致保留循环(dealloc 永远不会被调用)。如果不在 dealloc 方法中,我应该什么时候删除绑定?谢谢。
PS我知道绑定到代理对象控制器的方法,但我想避免使用如果可能的话。
这是我一直在做的一个例子:
// Top-level Project view
@interface ProjectViewController : NSViewController {
FoldersView *foldersView;
}
@property (strong) NSObjectController *projectObjectController; // holds Project instance
end
// Displays folders
@interface FoldersView : NSView {
FolderView *folderView;
}
@property (weak) NSObjectController *projectObjectController; // binded from parent
@property (strong) NSArrayController *foldersArrayController; // binded to project.folders
@end
// Displays selected folder
@interface FolderView : NSView
@property (weak) NSArrayController *foldersArrayController; // binded from parent
@property (strong) NSObjectController *folderObjectController; // binded to folders.selection
@end
I have an application where I need to access model data from my subviews. I've been using bindings to pass data across views; however, the bindings to self
seem to be causing retain cycles (dealloc never gets called). When should I remove the bindings if not in the dealloc method? Thanks.
P.S. I know the method of binding to a proxy object controller, but I'd like to avoid using it if possible.
Here's an example of what I've been doing:
// Top-level Project view
@interface ProjectViewController : NSViewController {
FoldersView *foldersView;
}
@property (strong) NSObjectController *projectObjectController; // holds Project instance
end
// Displays folders
@interface FoldersView : NSView {
FolderView *folderView;
}
@property (weak) NSObjectController *projectObjectController; // binded from parent
@property (strong) NSArrayController *foldersArrayController; // binded to project.folders
@end
// Displays selected folder
@interface FolderView : NSView
@property (weak) NSArrayController *foldersArrayController; // binded from parent
@property (strong) NSObjectController *folderObjectController; // binded to folders.selection
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定是从 MVC 三位一体中删除 C 部分(样板代码)的首选方式。所以你处理这个问题的方法是正确的。
The bindings are the preferred way of removing C part (boilerplate code) from the MVC trinity. So your approach to handling this problem is correct.