A presentation I delivered to ACCU in 2009 about using MVC (model-view-controller) in Cocoa to make best use of the frameworks and maximise code reuse.
3. Memory Lane
• 1980s: Brad Cox at StepStone adds Smalltalk
messaging to C. Some guy named Bjarne invents
“C with Classes”.
4. Memory Lane
• 1980s: Brad Cox at StepStone adds Smalltalk
messaging to C. Some guy named Bjarne invents
“C with Classes”.
• 1986: Cox authors Object Oriented Programming: an
evolutionary approach, ObjC is born
5. Memory Lane
• 1980s: Brad Cox at StepStone adds Smalltalk
messaging to C. Some guy named Bjarne invents
“C with Classes”.
• 1986: Cox authors Object Oriented Programming: an
evolutionary approach, ObjC is born
• 1988: NeXT licence ObjC from StepStone
6. Memory Lane
• 1980s: Brad Cox at StepStone adds Smalltalk
messaging to C. Some guy named Bjarne invents
“C with Classes”.
• 1986: Cox authors Object Oriented Programming: an
evolutionary approach, ObjC is born
• 1988: NeXT licence ObjC from StepStone
• 1993: GCC runtime for ObjC on !NeXT OS
7. Memory Lane
• 1980s: Brad Cox at StepStone adds Smalltalk
messaging to C. Some guy named Bjarne invents
“C with Classes”.
• 1986: Cox authors Object Oriented Programming: an
evolutionary approach, ObjC is born
• 1988: NeXT licence ObjC from StepStone
• 1993: GCC runtime for ObjC on !NeXT OS
• 1995: NeXT purchase all ObjC rights
8. Memory Lane
• 1980s: Brad Cox at StepStone adds Smalltalk
messaging to C. Some guy named Bjarne invents
“C with Classes”.
• 1986: Cox authors Object Oriented Programming: an
evolutionary approach, ObjC is born
• 1988: NeXT licence ObjC from StepStone
• 1993: GCC runtime for ObjC on !NeXT OS
• 1995: NeXT purchase all ObjC rights
• 1996: Apple (Computer, back then) acquire NeXT
9. Memory Lane
• 1980s: Brad Cox at StepStone adds Smalltalk
messaging to C. Some guy named Bjarne invents
“C with Classes”.
• 1986: Cox authors Object Oriented Programming: an
evolutionary approach, ObjC is born
• 1988: NeXT licence ObjC from StepStone
• 1993: GCC runtime for ObjC on !NeXT OS
• 1995: NeXT purchase all ObjC rights
• 1996: Apple (Computer, back then) acquire NeXT
• 2001: Mac OS X released
15. What did I do?
• Drew boxes in Interface Builder
16. What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
objects
17. What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
objects
• Automatically un-archived and displayed by
my app
18. What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
objects
• Automatically un-archived and displayed by
my app
• MVC score: F Should really try harder
28. Delegates
• Customise an object’s behaviour by making
decisions on its behalf
• GoF: Template Method (kindof)
29. Delegates
• Customise an object’s behaviour by making
decisions on its behalf
• GoF: Template Method (kindof)
• Implement a subset of delegate methods,
runtime introspection avoids exceptions
30. Delegates
• Customise an object’s behaviour by making
decisions on its behalf
• GoF: Template Method (kindof)
• Implement a subset of delegate methods,
runtime introspection avoids exceptions
• We’ll use WebFrameLoadDelegate
Who I am. What _are_ Objective-C and Cocoa? First, a walk down memory lane.
Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up.
Next: what exactly is MVC?
Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up.
Next: what exactly is MVC?
Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up.
Next: what exactly is MVC?
Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up.
Next: what exactly is MVC?
Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up.
Next: what exactly is MVC?
Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up.
Next: what exactly is MVC?
Objective-C was really popularised by NeXT on their NeXTSTEP OS. Note that there’s never been a language standard, but Apple owns all trademarks and drives the language definition. GCC support on other platforms plays catch-up.
Next: what exactly is MVC?
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
Well, we know it’s Model-View-Controller, I put that on the first slide. But what does each level do?
Next: into the sample.
I could do 45 of these in one session - it’s a shame there’s no market.
So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL.
Next: see what does really count as a model.
So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL.
Next: see what does really count as a model.
So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL.
Next: see what does really count as a model.
So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL.
Next: see what does really count as a model.
So we can see how Interface Builder works, but haven’t designed an MVC app at all. FAIL.
Next: see what does really count as a model.
Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
Brief description of the message-sending syntax and Objective-C 2.0 properties. Just enough so that the code samples make sense.
The only model object we really have is the URL. Let’s see how we can factor that out into an MVC design.
Next: class view.
So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty.
Next: move onto fixing the URL field, but discuss delegates first.
So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty.
Next: move onto fixing the URL field, but discuss delegates first.
So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty.
Next: move onto fixing the URL field, but discuss delegates first.
So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty.
Next: move onto fixing the URL field, but discuss delegates first.
So, we’ve separated things out, but that model object isn’t really used. The Controller is just filling it in out of a sense of duty.
Next: move onto fixing the URL field, but discuss delegates first.
Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
Some objects implement a basic behaviour then allow another object to interpose at specific points in the process. This is called delegation in Cocoa. It’s a bit like a Template Method, except that the delegate doesn’t encapsulate the original object. ObjC messaging means you can implement a subset of the delegate methods.
We can use a delegation point in the loading of a web page in order to get the address that the web view has really gone to. Oh, where’s the code to update the text field, though?
If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested.
Next: how that works in the browser app.
If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested.
Next: how that works in the browser app.
If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested.
Next: how that works in the browser app.
If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested.
Next: how that works in the browser app.
If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested.
Next: how that works in the browser app.
If a view (say) binds to a model, then the model is actually replaced by a proxy which posts observer notifications whenever changes are requested.
Next: how that works in the browser app.
So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.
Next: bookmarks.
So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.
Next: bookmarks.
So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.
Next: bookmarks.
So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.
Next: bookmarks.
So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.
Next: bookmarks.
So apart from triggering the “go” operation, the text field and web view are entirely decoupled. The model now has some utility.
Next: bookmarks.
Add the bookmarks. No need to change the model, just bind the new UI to a collection of web addresses.
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!
First: NSArrayController manages its content
Second: Add bookmark, note how information only passed over the controller layer
Third: Bookmark this page, again information flow over controller layer only
Next: Qs!