cocoa - ARC: How do you release a WindowController when the user closes the Window? -
i'm trying translate old code arc. old code in windowcontroller:
@interface preferencescontroller () <nswindowdelegate> @end @implementation preferencescontroller -(void)windowwillclose:(nsnotification*) notification { [self autorelease]; } @end
my appdelegate has strong pointer windowcontroller:
@property(strong) preferencescontroller* preferencesctrl;
in preferencescontroller, need declare (weak) pointer appdelegate, , this:
-(void) windowwillclose:(nsnotification *)notification { [[self appdelegate] setpreferencesctrl:nil]; }
well, thoughts right.
but can give make more simple.
set application delegate nswindowdelegate.
@interface appdelegate : nsobject <nsapplicationdelegate, nswindowdelegate> @property (strong) preferencescontroller* preferencesctrl; @end @implementation appdelegate - (void)doaction { // create window // ... self.preferencesctrl.window.delegate = self; // set window delegate } - (void)windowwillclose:(nsnotification *)notification { self.preferencesctrl=nil; } @end
upd since using nswindowdelegate methods, suggest create delegate protocol, preferencecontrollerdelegate
//in preferencecontroller.h before class interface @class preferencecontrollerdelegate @protocol preferencecontrollerdelegate <nsobject> - (void)preferencecontrollerwindowwillclose:(preferencecontrollerdelegate *)sender; @end @interface preferencecontroller : nswindowcontroller //... @property (nonatomic,weak) id<preferencecontrollerdelegate> delegate; //... @end
that proper.
Comments
Post a Comment