You’re previewing Ry’s Cocoa Tutorial

Events

So far, we’ve been using the target-action pattern as our primary tool for responding to user input. However, this is merely a high-level abstraction for Cocoa’s event architecture, which provides much more powerful tools for handling user input.

While an app is running, Cocoa constantly listens for mouse interactions and keystrokes. These are called events. When it detects one, it forwards the event to the view layer so it can respond appropriately. The only reason our beloved target-action pattern works is because the built-in UI components (namely, NSControl) automatically interpret these events and turn them into action method calls.

Collecting user input with the event architecture

In this chapter, we’ll learn how to manually capture mouse and key events. This will give us an idea of how the target-action pattern is actually implemented, and it will also give us quite a bit of experience creating custom views. With these skills, you’ll be able to build highly interactive apps like games and graphics editors, as well as your own reusable controls.

Keep in mind that everything we’re going to do in this chapter takes place entirely in the view layer. The MVC pattern hasn’t changed, and the target-action pattern is still the preferred way to communicate user input from the view layer back to the controller layer. We’re just learning about what’s going on under the hood in NSView.

Sorry, this is the end of the preview. To read the rest of this chapter, you’ll need to purchase Ry’s Cocoa Tutorial.

Mailing List

Sign up for my low-volume mailing list to find out when new content is released. Next up is a comprehensive Swift tutorial planned for late January.

You’ll only receive emails when new tutorials are released, and your contact information will never be shared with third parties. Click here to unsubscribe.