Chapter 8. Event Handling
Graphical interfaces are driven by user events—mouse clicks and keystrokes. However, a running application can also receive events that don’t originate from the user interface: packets arriving over a network interface, periodic timers firing, an input device being plugged into a USB port, or a CD being inserted into an attached drive. In an object-oriented world, both types of events eventually result in a message being sent to an object in your application, as depicted in Figure 8.1. This chapter focuses on events—both user- and program-generated—and how, as a programmer, you intercept, handle, and coordinate them in Cocoa.
Figure 8-1. A Cocoa application receiving events
Respond to User-Generated Events
In this section you’ll explore the structure of a Cocoa application from a user-event-handling perspective. You’ll learn about the classes in the core program framework and delve into topics such as the view hierarchy, the event cycle, and the responder chain. Later you’ll build a simple application that responds to a user’s mouse click by drawing a colored dot in the application’s window.
The Application Quartet
Four classes are at the core of a running application: NSResponder, NSWindow, NSView, and NSApplication (Figure 8.2). Each class plays a critical role in the two primary activities of an application: drawing the user interface and responding ...
Get Learning Cocoa now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.