How can I create a GUI and react to Cocoa events programmatically?

后端 未结 4 476
执笔经年
执笔经年 2020-12-28 11:35

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.

4条回答
  •  死守一世寂寞
    2020-12-28 11:56

    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:

    - (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.

提交回复
热议问题