I found out how to create a window in Cocoa programmatically but can't figure out how to react to events. The window is not reacting to a Quit request or button click.
我发现了如何以编程方式在Cocoa中创建窗口,但无法弄清楚如何对事件做出反应。该窗口不响应退出请求或按钮单击。
I tried adding the following controller and used setDelegate/setTarget without luck:
我尝试添加以下控制器并使用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" ];
> [ button setAction:@selector(doSomething:)];
> [ button setTarget:controller];
[ [ window contentView ] addSubview: button ];
[window makeKeyAndOrderFront:NSApp];
[[NSRunLoop currentRunLoop] run];
return 0;
}
4 个解决方案
#1
9
You need to invoke -[NSApplication run] instead of -[[NSRunLoop currentRunLoop] run]. The reason should be clear if you look at the basic structure of the method:
您需要调用 - [NSApplication run]而不是 - [[NSRunLoop currentRunLoop] run]。如果你看一下方法的基本结构,原因应该是清楚的:
- (void)run
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self finishLaunching];
shouldKeepRunning = YES;
do
{
[pool release];
pool = [[NSAutoreleasePool alloc] init];
NSEvent *event =
[self
nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
[self sendEvent:event];
[self updateWindows];
} while (shouldKeepRunning);
[pool release];
}
NSApplication encapsulates a lot about how to get an event, how to dispatch them and how to update windows.
NSApplication封装了很多关于如何获取事件,如何调度它们以及如何更新窗口的内容。
#2
3
I found out how to create a window in Cocoa programmatically …
我发现了如何以编程方式在Cocoa中创建一个窗口...
Why? Why not just make a nib?
为什么?为什么不做一个笔尖?
The window is not reacting to a Quit request or button click.
该窗口不响应退出请求或按钮单击。
How would you quit a window? This isn't Windows 3; applications can have multiple windows on Mac OS X. As such, closing a window and quitting an application are separate actions.
你怎么会退出窗户?这不是Windows 3;应用程序可以在Mac OS X上有多个窗口。因此,关闭窗口并退出应用程序是单独的操作。
[[NSRunLoop currentRunLoop] run];
[[NSRunLoop currentRunLoop] run];
Except in rare circumstances, running the run loop is NSApplication's job, and you should leave that to it. Use NSApplicationMain
or -[NSApplication run]
to tell the application to run.
除了在极少数情况下,运行运行循环是NSApplication的工作,你应该把它留给它。使用NSApplicationMain或 - [NSApplication run]告诉应用程序运行。
#3
2
Excellent question. I think Matt Gallagher answered it already, but if you want to go further with this, you'll have to delve into Apple's event-handling documentation. Bear in mind that doing everything programmatically will require a solid understanding of cocoa fundamentals.
好问题。我认为Matt Gallagher已经回答了这个问题,但如果你想进一步解决这个问题,你将不得不深入研究Apple的事件处理文档。请记住,以编程方式执行所有操作都需要对可可基础知识有充分的了解。
#4
0
I spent an entire day looking for answers to the GUI and Menu portion of this question. There are not that many current, concise answers out there to the question. So after solving it for myself, I posted an answer which addresses this on Stack here: Cocoa GUI Programmatically. I add a referral to it here to help community members who are digging around for the same answers.
我花了一整天的时间来寻找这个问题的GUI和菜单部分的答案。对于这个问题,目前没有那么多简洁的答案。所以在为自己解决之后,我发布了一个答案,在这里解决这个问题:Cocoa GUI以编程方式。我在这里添加了一个推荐,以帮助那些正在挖掘相同答案的社区成员。
#1
9
You need to invoke -[NSApplication run] instead of -[[NSRunLoop currentRunLoop] run]. The reason should be clear if you look at the basic structure of the method:
您需要调用 - [NSApplication run]而不是 - [[NSRunLoop currentRunLoop] run]。如果你看一下方法的基本结构,原因应该是清楚的:
- (void)run
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self finishLaunching];
shouldKeepRunning = YES;
do
{
[pool release];
pool = [[NSAutoreleasePool alloc] init];
NSEvent *event =
[self
nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
[self sendEvent:event];
[self updateWindows];
} while (shouldKeepRunning);
[pool release];
}
NSApplication encapsulates a lot about how to get an event, how to dispatch them and how to update windows.
NSApplication封装了很多关于如何获取事件,如何调度它们以及如何更新窗口的内容。
#2
3
I found out how to create a window in Cocoa programmatically …
我发现了如何以编程方式在Cocoa中创建一个窗口...
Why? Why not just make a nib?
为什么?为什么不做一个笔尖?
The window is not reacting to a Quit request or button click.
该窗口不响应退出请求或按钮单击。
How would you quit a window? This isn't Windows 3; applications can have multiple windows on Mac OS X. As such, closing a window and quitting an application are separate actions.
你怎么会退出窗户?这不是Windows 3;应用程序可以在Mac OS X上有多个窗口。因此,关闭窗口并退出应用程序是单独的操作。
[[NSRunLoop currentRunLoop] run];
[[NSRunLoop currentRunLoop] run];
Except in rare circumstances, running the run loop is NSApplication's job, and you should leave that to it. Use NSApplicationMain
or -[NSApplication run]
to tell the application to run.
除了在极少数情况下,运行运行循环是NSApplication的工作,你应该把它留给它。使用NSApplicationMain或 - [NSApplication run]告诉应用程序运行。
#3
2
Excellent question. I think Matt Gallagher answered it already, but if you want to go further with this, you'll have to delve into Apple's event-handling documentation. Bear in mind that doing everything programmatically will require a solid understanding of cocoa fundamentals.
好问题。我认为Matt Gallagher已经回答了这个问题,但如果你想进一步解决这个问题,你将不得不深入研究Apple的事件处理文档。请记住,以编程方式执行所有操作都需要对可可基础知识有充分的了解。
#4
0
I spent an entire day looking for answers to the GUI and Menu portion of this question. There are not that many current, concise answers out there to the question. So after solving it for myself, I posted an answer which addresses this on Stack here: Cocoa GUI Programmatically. I add a referral to it here to help community members who are digging around for the same answers.
我花了一整天的时间来寻找这个问题的GUI和菜单部分的答案。对于这个问题,目前没有那么多简洁的答案。所以在为自己解决之后,我发布了一个答案,在这里解决这个问题:Cocoa GUI以编程方式。我在这里添加了一个推荐,以帮助那些正在挖掘相同答案的社区成员。