简体   繁体   中英

How to prevent retain cycles caused by binding to self

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.

PS 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM