简体   繁体   English

如何在Cocoa / Objective-C中使用应用程序委托?

[英]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 : 我问了一个有关在窗口关闭时退出App的问题,答案是使用委托:

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? 我不确定如何将applicationShouldTerminateAfterLastWindowClosed放入applicationShouldTerminateAfterLastWindowClosed程序的委托中?

Your xcode project should have created an application delegate class when the project was created. 创建项目时,您的xcode项目应该已经创建了一个应用程序委托类。 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). 要在委托中实现此目的,唯一要做的就是将方法添加到委托.m文件中(您不必担心.h文件,因为该方法已被声明为应用程序委托协议的一部分。 )。

So you have the class called RubichevAppDelegate . 因此,您拥有名为RubichevAppDelegate的类。 Put the method into the .m file. 将方法放入.m文件中。

PS. PS。 You forgot the opening brace in your code. 您忘记了代码中的左括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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