简体   繁体   中英

How do I use application delegate in Cocoa/Objective-C?

I asked a question about quitting an App when windows close , and the answer was to use delegates :

There is a optional method for the application's delegate which will do this automatically. All you have to due is add this to the implementation. You don't need to create an outlet or anything.

 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication return YES; } 

It definitely works, but you may have put the code in the wrong place. Make sure it's in your application's delegate.

I'm not sure how can I put the applicationShouldTerminateAfterLastWindowClosed in the application's delegate?

Your xcode project should have created an application delegate class when the project was created. The only thing you need to do to implement this in your delegate is to add the method into the delegate .m file (you don't need to worry about the .h file since the method is already declared as part of the application delegate protocol).

So you have the class called RubichevAppDelegate . Put the method into the .m file.

PS. You forgot the opening brace in your code.

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