我发现了如何以编程方式在Cocoa中创建一个窗口,但无法弄清楚如何对事件做出反应.该窗口不响应退出请求或按钮单击.
我尝试添加以下控制器并使用setDelegate/setTarget而没有运气:
@interface AppController : NSObject {
}
- (IBAction)doSomething:(id)sender;
@end
@implementation AppController
- (IBAction)doSomething:(id)sender;
{
printf("Button clicked!\n");
}
@end
int main(int argc, char **args){
NSRect frame = NSMakeRect(0, 0, 200, 200);
AppController *controller = [[AppController alloc] init];
> [[NSApplication sharedApplication] setDelegate:controller];
NSWindow* window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window setBackgroundColor:[NSColor blueColor]];
NSButton *button = [ [ NSButton alloc ] initWithFrame: NSMakeRect( 30.0, 20.0, 80.0, 50.0 ) ];
[ button setBezelStyle:NSRoundedBezelStyle];
[ button setTitle: @"Click" ];
> [ …Run Code Online (Sandbox Code Playgroud) 我正在为 mac 创建一个时间跟踪器应用程序。我使用以下代码在全屏模式下运行该应用程序,它运行良好。
[[self.window contentView] enterFullScreenMode:[NSScreen mainScreen] withOptions:nil]
Run Code Online (Sandbox Code Playgroud)
但仍然可以使用 cmd+q 关闭应用程序。有什么办法可以防止我关闭应用程序吗?