Introduction to Cocoa Application
Development
Module 6: AppKit Classes, Selectors, Timers, Delegates,
Panels and Sheets
All Materials © 2008 Apple Inc. All Rights Reserved.
AppKit Classes Overview
NSApplication
■ Manages the application lifecycle
■ Runs the main event loop of the application
■ Manages the main menu bar
■ Keeps track of windows in the app
■ Uses delegate to customize behaviors
■ Rarely subclassed
NSWindow
■ Manages the frame of the window
■ Keeps track of the views inside the window
■ Dispatches events to appropriate view
■ Coordinates drawing of views
NSView
■ Manages a “rectangle” in a window
■ Handles events directed at it
■ Draws the contents of itself
■ Always subclassed
■ Views are arranged in a tree
- Ordering is back-to-front; children lie on top of parents
NSControl
■ Handles user input and events
■ Sends action messages
■ Intended to be subclassed for new control types
- NSButton, NSTextField, NSBrowser
■ Works in conjunction with NSCell to do drawing
NSFormatter
■ Used to affect data display
- Converts value to a string and back again
■ Attached to NSTextFields or NSTableColumns
using Interface Builder
■ Two Built-In Formatters
- NSDateFormatter, NSNumberFormatter
- Custom formatters can be written fairly easily
■ Formatters are part of Foundation
Exercise 6.1
■ Working with Formatters
Selectors
Selectors identify methods by name
■ A selector has type SEL
SEL action = [button action];
[button setAction:@selector(convert:)];
■ Selectors include message name and all colons
-(void)setName:(NSString *)name age:(int)age;
SEL sel = @selector(setName:age:);
■ Conceptually similar to a function pointer
Working with selectors
■ At runtime, you can determine if an object
responds to a given selector
id obj;
SEL sel = @selector(run);
if ([obj respondsToSelector:sel]) {
[obj performSelector:sel];
}
■ Passing parameters with performSelector:
id obj;
SEL sel = @selector(setName:);
if ([obj respondsToSelector:sel]) {
[obj performSelector:sel withObject:@”Michael”];
}
Timers
Timer
■ Way to perfo