Chapter 4. Using the delegate pattern
This chapter covers
- How delegates work
- Creating your own delegate methods
- Mastering WebView delegates
If you’ve done any Cocoa development, chances are you’ve already used the delegate pattern. If you haven’t, this chapter will explain the important role delegation plays in MacRuby Cocoa application development. Delegation is a common design pattern for object-oriented programming languages. The Cocoa framework makes heavy use of delegation, which makes it a vital concept to understand if you plan to write MacRuby Cocoa applications.
In this chapter, you’ll get a good understanding of delegates, including how to use them, how to create your own, and how to use them in your applications. You’ll even get a chance to build a MacRuby web browser using the knowledge you’ve gained about delegates.
A delegate is an object that responds to or acts upon a certain event or action that another object called. The object that triggers the call to a delegate is referred to as the delegating or responder object. Full responsibility is given to the delegate to decide how to respond to a certain event.
You typically use delegates to deal with actions that involve the user interface. If you feel confused, don’t worry; in this section, we’re going to explain where you’ve used delegates before and how they work, and then we’ll walk you through some delegate method examples.