Episode 7 - Helper Objects

Episode 7 - Helper Objects

Posted 11/26/2006 - 10:09 by cocoacast

helpwantedThe main goal of this Podcast is to discuss the major difference between subclassing and use of helper objects. We will start to talk about is-a vs. has-a relationship and continue this topic in the next podcast. We will talk about Delegates and NSTableView and how it is different from the table views used by other frameworks. We finish with more advanced topic of responding to a selector and how it is used by delegates.



Here are the show notes for this Episode: Show Notes
View the Podcast

Show Notes:

Helper Objects
Episode 7
We will talk about Objective-C’s way of extending objects
Chapter 5 - Helper Objects
Helper Objects
Episode 7
We will talk about Objective-C’s way of extending objects
Chapter 5 - Helper Objects

Podcast Announcements
• Long Delay - Sorry
• Quality Warning
• Get Back on Schedule
• Your Help if Always Appreciated
Podcast Announcements
• Long Delay - Sorry
• Quality Warning
• Get Back on Schedule
• Your Help if Always Appreciated

Recap of the Last Episode
Summary of the Episode 6
Recap of the Last Episode
Summary of the Episode 6

Summary of Episode 6
• Controls
• Intro to Controls and Class Hierarchy
• NSButton, NSSlider, and NSTextField
• Intro to MVC by SpeakLine Example
• NSCollorWell
• Target and Action Programatically
Summary of Episode 6
• Controls
• Intro to Controls and Class Hierarchy
• NSButton, NSSlider, and NSTextField
• Intro to MVC by SpeakLine Example
• NSCollorWell
• Target and Action Programatically

In this Episode
• Difference between Subclassing and Helper Objects
• Delegates
• NSTableView and dataSouce
• How Delegates Work (more details)
In this Episode
• Difference between Subclassing and Helper Objects
• Delegates
• NSTableView and dataSouce
• How Delegates Work (more details)

Display a size of word typed by a user - GUI + Objective-C challenge
Solution to Challenge in Episode 6
Display a size of word typed by a user - GUI + Objective-C challenge
Solution to Challenge in Episode 6

Two different ways of extending classes (objects) in Objective-C
Subclassing and Helper Objects
Two different ways of extending classes (objects) in Objective-C
Subclassing and Helper Objects

Subclassing vs. Helper Objects
• Robocop vs. Kitt
Vs.
Subclassing vs. Helper Objects
• Robocop vs. Kitt
Vs.

Subclassing vs. Helper Objects
• IS-a vs. HAS-a Relationship
VS.
Subclassing vs. Helper Objects
• IS-a vs. HAS-a Relationship
VS.

Class Hierarchy
NSObjectNSResponderNSViewNSVindowNSControlstringValue : NSString *
NSTextFieldstate : BOOLNSButtonfloatValue : floatNSSliderIs an object:
init, retain, release, dealloc, etc.
Hnadles events:
mouseDown:, keyDown:, etcAppears in a window:
drawRect:, window, etcHas a target and an action:
setTarget:, setAction:, etc.
Class Hierarchy
NSObjectNSResponderNSViewNSVindowNSControlstringValue : NSString *
NSTextFieldstate : BOOLNSButtonfloatValue : floatNSSliderIs an object:
init, retain, release, dealloc, etc.
Hnadles events:
mouseDown:, keyDown:, etcAppears in a window:
drawRect:, window, etcHas a target and an action:
setTarget:, setAction:, etc.

Class Hierarchy Class Hierarchy

Cocoa’s way of Extension by setting an outlet pointing to a Helper Object
Delegates
Cocoa’s way of Extension by setting an outlet pointing to a Helper Object
Delegates

Using Delegates
• Implementing Delegate Methods
• Setting Delegate Outlet
NSSpeechSynthesizer-(void)speechSynthesizer:
didFinishSpeaking:
-(void)speechSynthesizer:
willSpeakWord:
ofString:
-(void)speechSynthesizer:
willSpeakPhoneme:
Using Delegates
• Implementing Delegate Methods
• Setting Delegate Outlet
NSSpeechSynthesizer-(void)speechSynthesizer:
didFinishSpeaking:
-(void)speechSynthesizer:
willSpeakWord:
ofString:
-(void)speechSynthesizer:
willSpeakPhoneme:

Speaking Example with Delegates
Demo
Speaking Example with Delegates
Demo

Helper objects for displaying data in Tables
NSTableView and dataSource
Helper objects for displaying data in Tables
NSTableView and dataSource

• NSTableView has an outlet -- dataSource
• There two (or three) methods you need to implement
NSTableView and Its dataSource
- (int)numberOfRowsInTableView:(NSTableView *)aTableView;
- (id)tableView:(NSTableView *)aTableViewobjectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex;
- (void)tableView:(NSTableView *)aTableViewsetObjectValue:(id)anObjectforTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex;
• NSTableView has an outlet -- dataSource
• There two (or three) methods you need to implement
NSTableView and Its dataSource
- (int)numberOfRowsInTableView:(NSTableView *)aTableView;
- (id)tableView:(NSTableView *)aTableViewobjectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex;
- (void)tableView:(NSTableView *)aTableViewsetObjectValue:(id)anObjectforTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex;

NSTableView and dataSource
Demo
NSTableView and dataSource
Demo

• Misspessing the name of the method.
You will not get any error or warning from compiler.
Copy and paste the declaration from documentation.
• Forgetting to set the Delegate outlet.
You will not get any error or warning from the compiler if you make this
error
Common Errors in implementing Delegates
• Misspessing the name of the method.
You will not get any error or warning from compiler.
Copy and paste the declaration from documentation.
• Forgetting to set the Delegate outlet.
You will not get any error or warning from the compiler if you make this
error
Common Errors in implementing Delegates

Common Classed with Delegates

NSAllert NSTextStorageNSApplicationNSTextView
NSBrowser NSToolbar
NSDrawer NSWindow
NSFontManagerNSImageNSLayoutManagerNSMatrix
NSMenu
NSSavePanel
NSSound
NSSpeechRecorderNSSpeechSynthesizerNSSplitViewNSText
NSTextField

More Details about How Delegates Work
How Delegates Work
More Details about How Delegates Work
How Delegates Work

• NSObject has the following method (see below)
• Messages are sent if delegate implements the method
• Results of respondsToSelector: are cached for better performance
How Delegates Work
- (BOOL)respondsToSelector:(SEL)aSelector
• NSObject has the following method (see below)
• Messages are sent if delegate implements the method
• Results of respondsToSelector: are cached for better performance
How Delegates Work
- (BOOL)respondsToSelector:(SEL)aSelector

Restricting Window Resizing using Delegates
Chapter 5 Challenge
Restricting Window Resizing using Delegates
Chapter 5 Challenge

Summary
• Difference between Subclassing and Helper Objects
• Delegates
• NSTableView and dataSouce
• How Delegates Work (more details)
Summary
• Difference between Subclassing and Helper Objects
• Delegates
• NSTableView and dataSouce
• How Delegates Work (more details)

Next Time
• Solution to Challenge from Chapter 5
• Chapter 6 Bindings and NSController
• Key-Value Coding
• NSController
• Bindings
• Sorting
• It may require Two Episodes to Cover
Next Time
• Solution to Challenge from Chapter 5
• Chapter 6 Bindings and NSController
• Key-Value Coding
• NSController
• Bindings
• Sorting
• It may require Two Episodes to Cover

Happy Coding Everybody Happy Coding Everybody

Trackback URL for this post: http://www.cocoacast.com/?q=trackback/20
4
Your rating: None Average: 4 (1 vote)