using and customizing cocoa bindings - progsocwhophd/stavros/416-slides.pdfcontroller model...

Post on 22-Apr-2018

236 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Movie to come

Session 416

Using and Customizing Cocoa Bindings

Ron Lue-SangGlue Code Architect

Using and Customizing Cocoa Bindings

• Overview of Cocoa Bindings• New features• Tips and tricks– Roll your own bindings

Agenda

Controller

Model

ViewAppKit

Glue Code

Foundation Core Data

Cocoa Bindings

Note: this slide has a build whose order must be retained

Model View Controller

FileMerge Testimonial

MVC the Cocoa Way• Re-usable controller classes• Binding – Attribute of one object– Property of another

DataObject

DataObject

DataObject

DataObject

DataObject

NSArrayController

Array

Content

Text Field

Value binding

selection.name

Technology Base for Cocoa Bindings

Key Value BindingKey Value ObservingKey Value Coding

Cocoa Bindings

Key Value Coding Key Value Observing Key Value Binding

Key Value Coding- (id)valueForKey:(NSString *)key- (void)setValue:(id)value forKey:(NSString *)key

M V

CKVCKV

C

Key Value Observing- (void) addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath

options:(NSKeyValueObservingOptions)options context:(void *)context

selection.name

M V

CKVOKV

O

Key Value Observing- (void)observeValueForKeyPath:(NSString *)keyPath

ofObject:(id)object change:(NSDictionary *)change context:(void *)context;

M V

CKVOKV

O

selection.name

Key Value Binding- (void) bind:(NSString *)binding toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary *)options

M

C

V

KVBKVB

VKVB

Edit Cycle• Key Value Coding

M V

CsetValueFor

KeyPath:setVa

lueFor

KeyPa

th:

Edit Cycle• Key Value Observing

M V

C

observe

Value

ForKey

Path

:

V2observeValue

ForKeyPath:

Binding in Interface Builder

Demo

New Features in Tiger

Controllers• NSTreeController– NSOutlineView– NSBrowser• Filtering NSArrayController– No subclassing needed

Tree Controller

NSTreeController• Like NSArrayController and arrays– Manages a tree of objects• Your data’s tree structure– NSArrays in KVC objects in NSArrays

NSArray

children

ModelObject

ModelObject

ModelObject

ModelObject

ModelObject

ModelObject

NSArray

NSArray

children

childrenNSArray

NSArray

children

ModelObject

NSTreeController• Describe your tree– childrenKey– Optional– countKey– leafKey

• Change your tree– insert:– add:– insertChild:– addChild:– remove:

NSTreeController• Setting controller content– contentObject binding– contentObject outlet• Controller Keys– arrangedObjects– selection– canInsertChild– canAddChild– selectionIndexPaths

NSIndexPath• Identifier for trees• Similar to index of array• Represents a path of indexes

(2, 4, 9)

NSTreeController• NSOutlineView column bindings– Like columns in NSTableView

NSTreeController• NSOutlineView bindings– content– selectionIndexPaths

NSTreeController• NSBrowser bindings– content– selectionIndexPaths– contentValues

NSOutlineView and NSBrowser•WWDC Caveat– No edit support via bindings

Filtering

NSArrayController, NSPredicate, NSSearchField

Filtering• NSArrayController• NSArray• Synergized! NSSearchField

NSPredicate• Courtesy of Core Data• Describes a condition

• NSArray filtering

• NSMutableArray filtering

(name == "Ron")(name beginsWith "R") && (occupation == "Jamaican")

- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate

- (void)filterUsingPredicate:(NSPredicate *)predicate

NSArrayController Filtering• setFilterPredicate:

• filterPredicate controller key• Clears filter predicate on insertion• arrangedObjects represents the filtered content array– Doesn’t delete your objects

- (void)setFilterPredicate:(NSPredicate *)predicate

NSSearchField Bindings• recentSearches– Like Safari

NSSearchField Bindings• predicate (predicate2, predicate3, ...)– Like Mail

NSSearchField Bindings• predicate (predicate2, predicate3, ...)– Like Mail– Bind to arrayController.filterPredicate– 1 binding per menu item

NSDisplayNameBindingOptionNSPredicateFormatBindingOption

Core Data Integration

Core Data Support • Core Data support added to controllers– NSObjectController– NSArrayController

Core Data• You define a description of your data (model)– entity → class• NSManagedObjectContext– Manages object use (working set)– Manages object storage/retrieval• Fetch objects by entity– NSPredicate for which objects to fetch

NSControllers and Core Data• Define the entity of the controller– As opposed to object class• Define a fetch predicate– Not the same as filterPredicate• New bindings and outlets– managedObjectContext

• Creates a fetch request for you– Fetch automatically– fetch:

NSControllers and Core Data• Sort in the array controller• NSArrayController contentSet binding• KVO means we’re always in sync

Demo

Other New Features

Binding Debug Mode• Defaults write “NSBindingDebugLogLevel” 1• Help catch configuration errors– Error accessing, setting, invoking

Binding Info• Dictionary of information about a binding– NSObjectObservedKey—object bound to– NSObservedKeyPathKey—key path bound with– NSOptionsKey—dictionary of options

- (NSDictionary *)infoForBinding:(NSString *)binding

NSArrayController• Always use Multiple Selection Marker option– NSMultipleSelectionMarker– Performance optimization for LARGE, similar selections

New Bindings and Options

NSView toolTip

NSTextView attributedString

NSWindow displayPatternTitle

NSTableColumn Creates sort descriptor binding option

NSArrayController contentSet

NSPopup and NSMatrix Bindings• contentObjects• NSMatrix mode changes– Radio (Single selection)– selectedValue– selectedObject– List and Highlight (Multi selection)– selectedValues– selectedObjects

Tips and Tricks

Including rolling your own bindings

Roll Your Own Bindings

Make your Class Cocoa Bindings friendly

Your Model and Cocoa Bindings• KVC compliant attribute– KVC accessors– ivar access• KVO compliance– KVO auto-notification– Manual notification– willChangeValueForKeyPath:– didChangeValueForKeyPath:

• KVB exposing– exposeBinding:

Your View and Cocoa Bindings• KVB implementation– bind:toObject:withKeyPath:options:– unbind:• KVO support– Add yourself as an observer– Respond to observer message– observeValueForKeyPath:ofObject:change:context:

Editing Support• NSEditor (messages for you to respond to)– commitEditing– discardEditing• NSEditorRegistration (messages for you to send)– objectDidBeginEditing:– objectDidEndEditing:

Interface Builder Support• Build a palette– Promotes code reuse

Key Value Coding (KVC)- (void)setName:(id)name;- (id)name;

Key Value Binding (KVB)- (void) bind:(NSString *)binding

toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary *)options

- (void) unbind:(NSString *)binding

Key Value Binding (KVB)- (void) bind:... {id observedController;id observedKeyPath;id bindingOptions;

}

Key Value Observing (KVO)- (void) addObserver:(id)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context

- (void) removeObserver:(id)observer forKeyPath:(NSString *)keyPath

Key Value Observing (KVO)- (void)observeValueForKeyPath:(NSString *)keyPath

ofObject:(id)object change:(NSDictionary *)change context:(void *)context;

Demo

Recap• Deep breath•MVC– Code reuse– Reusable controller layer for Cocoa– NSController subclasses– Binding objects

• New features– NSTreeController– Filtering– Core Data– Bindings and binding options

Recap• Deep breath• Tips and tricks– Bindings– Bindings options– Validation, coercion, transformation• Roll your own bindings– KVC, KVO, KVB– Model class support– View class support

Documentation & Sample Code

Sample Code & Demosconnect.apple.com: Download Software → WWDC 2004 Application Technologies 416

Cocoa BindingsADC Home > Reference Library > Cocoa

Cocoa Bindings ReferenceADC Home > Reference Library > Cocoa

Reference Library

Who To Contact

Matt Formica Cocoa & Development Tools Evangelistmformica@apple.com

Q&A

Malcolm CrawfordTechnical Writer

top related