Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Adopting Model-View-
 Controller in Cocoa
  and Objective-C
             Graham Lee
     http://twitter.com/iamleeg
Memory Lane
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with ...
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with ...
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with ...
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with ...
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with ...
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with ...
Memory Lane
•   1980s: Brad Cox at StepStone adds Smalltalk
    messaging to C. Some guy named Bjarne invents
    “C with ...
What does MVC mean?
What does MVC mean?
          Display information




          Prepare content




                  Provide information
What does MVC mean?
   Respond to events      Display information




  Trigger service logic   Prepare content




 Do th...
Demo - one-minute
    browser
What did I do?
What did I do?
• Drew boxes in Interface Builder
What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
  objects
What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
  objects
• Automatically un-ar...
What did I do?
• Drew boxes in Interface Builder
• Automatically created and archived View
  objects
• Automatically un-ar...
On syntax
On syntax

• [anObject   sendMessage: parameter];
On syntax

• [anObject sendMessage: parameter];
• @property int count;
• int x = anObject.count;
• anObject.count = x * 2;
Demo - model the URL
MVC Score: D Principles not really applied
Delegates
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
• GoF: Template Method (kindof)
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
• GoF: Template Method (kindof)
• Implemen...
Delegates
• Customise an object’s behaviour by making
  decisions on its behalf
• GoF: Template Method (kindof)
• Implemen...
Demo - fix the address
        bar
Demo - fix the address
        bar
     (oh, and add window titles)
Cocoa Bindings
Cocoa Bindings


  «bind»
Cocoa Bindings


  «bind»   «observe»
MVC Score: B Good, but I want bookmarks ☺
Demo - bookmarks
MVC Score: A That’ll do for now!
?
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Adopting MVC in Cocoa
Upcoming SlideShare
Loading in …5
×

Adopting MVC in Cocoa

7,553 views

Published on

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.

Published in: Technology, Self Improvement
  • Be the first to comment

Adopting MVC in Cocoa

  1. 1. Adopting Model-View- Controller in Cocoa and Objective-C Graham Lee http://twitter.com/iamleeg
  2. 2. Memory Lane
  3. 3. Memory Lane • 1980s: Brad Cox at StepStone adds Smalltalk messaging to C. Some guy named Bjarne invents “C with Classes”.
  4. 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. 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. 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. 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. 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. 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
  10. 10. What does MVC mean?
  11. 11. What does MVC mean? Display information Prepare content Provide information
  12. 12. What does MVC mean? Respond to events Display information Trigger service logic Prepare content Do the work Provide information
  13. 13. Demo - one-minute browser
  14. 14. What did I do?
  15. 15. What did I do? • Drew boxes in Interface Builder
  16. 16. What did I do? • Drew boxes in Interface Builder • Automatically created and archived View objects
  17. 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. 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
  19. 19. On syntax
  20. 20. On syntax • [anObject sendMessage: parameter];
  21. 21. On syntax • [anObject sendMessage: parameter]; • @property int count; • int x = anObject.count; • anObject.count = x * 2;
  22. 22. Demo - model the URL
  23. 23. MVC Score: D Principles not really applied
  24. 24. Delegates
  25. 25. Delegates • Customise an object’s behaviour by making decisions on its behalf
  26. 26. Delegates • Customise an object’s behaviour by making decisions on its behalf • GoF: Template Method (kindof)
  27. 27. 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
  28. 28. 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
  29. 29. Demo - fix the address bar
  30. 30. Demo - fix the address bar (oh, and add window titles)
  31. 31. Cocoa Bindings
  32. 32. Cocoa Bindings «bind»
  33. 33. Cocoa Bindings «bind» «observe»
  34. 34. MVC Score: B Good, but I want bookmarks ☺
  35. 35. Demo - bookmarks
  36. 36. MVC Score: A That’ll do for now!
  37. 37. ?

×