I set a NSViewController’s view as contentview of mainmenu’s window,now how should i use – (BOOL)windowShouldClose:(id)sender ?
i use <NSWindowDelegate> in @interface and put - (BOOL)windowShouldClose:(id)sender on .m but doesn’t work
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
-(BOOL)windowShouldClose:(id)senderonly gets sent to the delegate of the window that will be sending the message. (if you’re not crystal clear on what delegates are, they’re kind of like the army commander that everybody reports to)Here, in order to be able to use
-windowShouldClose:in yourNSViewController, you need to set theNSViewControlleras the window’sdelegate. There’s two steps to this:Make it possible for the ‘NSViewController’ to be the window
delegate. Usually the window’s delegate is the
NSWindowController(it’s named that for a reason), but if you want to make
‘NSViewController’ the delegate you need to use the
<NSWindowDelegate>protocol, which you’ve already done.Actually set the view controller as the delegate. As Ken Thomases
noted in his comment, you can do this in Interface Builder by
dragging the window’s
delegateoutlet and connecting it to yourNSViewController. Or you could do it programmatically:
[self.view.window setDelegate:self]as you mentioned should work.