Top Banner
Movie to come Session 416 Using and Customizing Cocoa Bindings
63

Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Apr 22, 2018

Download

Documents

doque
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Movie to come

Session 416

Using and Customizing Cocoa Bindings

Page 2: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Ron Lue-SangGlue Code Architect

Using and Customizing Cocoa Bindings

Page 3: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Agenda

Page 4: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Controller

Model

ViewAppKit

Glue Code

Foundation Core Data

Cocoa Bindings

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

Model View Controller

Page 5: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

FileMerge Testimonial

Page 6: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 7: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Technology Base for Cocoa Bindings

Key Value BindingKey Value ObservingKey Value Coding

Cocoa Bindings

Key Value Coding Key Value Observing Key Value Binding

Page 8: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

M V

CKVCKV

C

Page 9: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

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

selection.name

M V

CKVOKV

O

Page 10: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

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

M V

CKVOKV

O

selection.name

Page 11: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

M

C

V

KVBKVB

VKVB

Page 12: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Edit Cycle• Key Value Coding

M V

CsetValueFor

KeyPath:setVa

lueFor

KeyPa

th:

Page 13: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Edit Cycle• Key Value Observing

M V

C

observe

Value

ForKey

Path

:

V2observeValue

ForKeyPath:

Page 14: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Binding in Interface Builder

Page 15: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Demo

Page 16: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

New Features in Tiger

Page 17: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 18: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Tree Controller

Page 19: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 20: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

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

Page 21: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 22: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

(2, 4, 9)

Page 23: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

NSTreeController• NSOutlineView column bindings– Like columns in NSTableView

Page 24: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

NSTreeController• NSOutlineView bindings– content– selectionIndexPaths

Page 25: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

NSTreeController• NSBrowser bindings– content– selectionIndexPaths– contentValues

Page 26: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 27: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Filtering

NSArrayController, NSPredicate, NSSearchField

Page 28: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Filtering• NSArrayController• NSArray• Synergized! NSSearchField

Page 29: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 30: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 31: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

NSSearchField Bindings• recentSearches– Like Safari

Page 32: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 33: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

NSDisplayNameBindingOptionNSPredicateFormatBindingOption

Page 34: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Core Data Integration

Page 35: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 36: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 37: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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:

Page 38: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 39: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Demo

Page 40: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Other New Features

Page 41: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 42: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 43: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 44: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

New Bindings and Options

NSView toolTip

NSTextView attributedString

NSWindow displayPatternTitle

NSTableColumn Creates sort descriptor binding option

NSArrayController contentSet

Page 45: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 46: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Tips and Tricks

Including rolling your own bindings

Page 47: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Roll Your Own Bindings

Make your Class Cocoa Bindings friendly

Page 48: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

• KVB exposing– exposeBinding:

Page 49: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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:

Page 50: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 51: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Interface Builder Support• Build a palette– Promotes code reuse

Page 52: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 53: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

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

- (void) unbind:(NSString *)binding

Page 54: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

}

Page 55: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

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

Page 56: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

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

Page 57: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Demo

Page 58: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 59: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 60: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

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

Page 61: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Who To Contact

Matt Formica Cocoa & Development Tools [email protected]

Page 62: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose

Q&A

Malcolm CrawfordTechnical Writer

Page 63: Using and Customizing Cocoa Bindings - ProgSocwhophd/stavros/416-slides.pdfController Model AVppKiewit Glue Code Foundation Core Data Cocoa Bindings Note: this slide has a build whose