Slide 1

Slide 1 text

iOS (7) Workshop

Slide 2

Slide 2 text

Model-View-Controller View Model Controller Update User Action Notify Update

Slide 3

Slide 3 text

Model View Controller

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

V M C Views • Defines a rectangular region on the screen and handles the drawing and touch events in that region. • Every application has at least one view (the window) for presenting it’s content. • A view can also act as a parent for other views and coordinate the placement and sizing of those views. • Examples: Buttons, Navigation Bars, Alerts, Table View Cells, etc...

Slide 6

Slide 6 text

View Controllers • Each view controller organizes and controls a view. • A view controller owns it’s view. It takes care of all user actions, animations, updating the view, etc... • Just like Views, Controllers can act as parents of other Controllers. V M C

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Navigation Controller • A Controller that manages the navigation of hierarchical content. • You can push/pop view controllers into/from the Navigation Controller. • Each time you add a view controller to the hierarchy the Navigation Controller becomes it’s parent. V M C

Slide 9

Slide 9 text

Collection View Controller • Contains and manages it’s collection view (each controller has a root view, remember?). • Usually you subclass it to add your custom behavior. • By default it is the delegate and datasource of it’s collection view. V M C

Slide 10

Slide 10 text

Delegation • Or acting on behalf/at the request of another object. • The delegating object sends a message to it’s delegate telling it some event is about to happen and asks for some response. • Delegation is a means for injecting specific behavior in the workings of a framework class — without having to subclass it. V M C

Slide 11

Slide 11 text

Delegate

Slide 12

Slide 12 text

User taps Delegate

Slide 13

Slide 13 text

User taps shouldSelectItemAtIndexPath: Delegate

Slide 14

Slide 14 text

User taps shouldSelectItemAtIndexPath: Delegate YES

Slide 15

Slide 15 text

Data Source • It’s like a delegate except that, instead of being delegated control of the UI, it is delegated control of the data. • Responsible for managing the memory of the model objects they give to the delegating view. V M C

Slide 16

Slide 16 text

Data Source

Slide 17

Slide 17 text

View starts loading Data Source

Slide 18

Slide 18 text

View starts loading numberOfSectionsInCollectionView: Data Source

Slide 19

Slide 19 text

View starts loading numberOfSectionsInCollectionView: Data Source 1

Slide 20

Slide 20 text

View starts loading Data Source numberOfItemsInSection:0

Slide 21

Slide 21 text

View starts loading Data Source numberOfItemsInSection:0 7

Slide 22

Slide 22 text

View starts loading Data Source cellForItemAtIndexPath:

Slide 23

Slide 23 text

View starts loading Data Source cellForItemAtIndexPath: Cell View

Slide 24

Slide 24 text

View starts loading Data Source cellForItemAtIndexPath: Cell View

Slide 25

Slide 25 text

Application Delegate • A custom object created for you at app launch time. • It’s primary job is to handle state transitions within the app. • Example: application:didFinishLaunching:

Slide 26

Slide 26 text

Storyboards V M C • Big canvas where you lay out your app screens and transitions • Trees of view controllers in a serialized form • Storyboards = Scenes (VCs) + Segues (transitions) • Good for a conceptual overview of the app • Bad for apps with lots of VCs or iPad apps (HUGE views...) • Limitations: Storyboards < XIBs < Code

Slide 27

Slide 27 text

Objective-C • 1983 • Stepstone -> NeXT -> Apple • ObjC = C + Smalltalk • Object-oriented, reflective • OSX (Cocoa), iOS (Cocoa Touch)

Slide 28

Slide 28 text

Basics Objective-C Java char b = 0; byte b = 0; int i = 0; int i = 0; float f = 0; float f = 0; double d = 0; double d = 0; BOOL b = YES; // NO boolean b = true; // false char c = ‘c’; char c = ‘c’; NSObject *o = [[NSObject alloc] init]; Object o = new Object(); NSString *s = @”string”; String s = “string”;

Slide 29

Slide 29 text

Basics Objective-C Java #import “MyType.h” import me.app.MyType; - (void)method; public void method() { ... } + (void)method; static public void method() { ... } const, (...readonly...) final static static package nil null

Slide 30

Slide 30 text

Basics • (Almost) all C/C++-stuff (if, for, while,...) • Pointers (*) to objects. • Pointer = reference to another value in memory [instance message:argument1 otherParameter:argument2];

Slide 31

Slide 31 text

Stuff 0x3DE2FE Basics

Slide 32

Slide 32 text

Stuff 0x3DE2FE Basics

Slide 33

Slide 33 text

Stuff 0x3DE2FE Basics

Slide 34

Slide 34 text

Object * Stuff 0x3DE2FE Basics

Slide 35

Slide 35 text

.h & .m • ObjC type = interface + implementation • .h: header file, type contract to the outside world @implementation MyType - (void)myMessage:(int)myParam { // Do stuff... } @end @interface MyType - (void)myMessage:(int)myParam; @end • .m: messages (old stuff) file, implementation (actual code)

Slide 36

Slide 36 text

Messages • Message: just a fancy name for a method call • Interpreted in runtime: objects decide if they respond to messages • self = this object (can message self) • super = base (parent) object (can message super)

Slide 37

Slide 37 text

Properties • Syntactic sugar on instance variables • Clang generates setters and getters automatically (it was not always like this) • Atomic by default! (use nonatomic to remove lock) @property (nonatomic, copy) NSString *myProperty;

Slide 38

Slide 38 text

• Protocol: just a fancy name for an interface (defines an expected behaviour) • Messages and properties can be mandatory or optional • Used in the delegate pattern: “will ask somebody (the delegate) something” Protocols

Slide 39

Slide 39 text

@interface MyType : NSObject @end @protocol MyProtocol - (void)myMandatoryMessage; @optional - (void)myOptionalMessage; @end Protocols

Slide 40

Slide 40 text

Categories • Category: allows to extend a class without inheritance • Add new messages without recompile! • Warning: existing messages can be “replaced”. Category messages take precedence. • Similar to .NETs extension methods

Slide 41

Slide 41 text

Categories @implementation UIColor (MyColors) + (UIColor*)myAwesomeColor { return ...; } + (UIColor*)blackColor { // I told ya... return [UIColor redColor]; } @end @interface UIColor (MyColors) + (UIColor*)myAwesomeColor; + (UIColor*)blackColor; // Oops! @end

Slide 42

Slide 42 text

Collections • Store “things” • Jumble! (can mix Strings with Numbers with Astrocreeps...) • Arrays, Dictionary, Sets • Immutable: once set can’t change (NSArray, NSDictionary, NSSet) • Mutable: can change (NSMutableArray, NSMutableDictionary, NSMutableSet) • Sort, filter, query, enumerate, map & other cool stuff

Slide 43

Slide 43 text

Memory • Instantiate new object: MyType *myType = [[MyType alloc] init]; • Reference count: keep track of the number of instances. If 0 deallocate object • ARC does retain/release for you automatically. • References: strong (retain), weak, copy (new immutable object)

Slide 44

Slide 44 text

Blocks • Closures: fancy name for functions that can be passed around like data • Key to lots of ObjC features: collection enumeration, Grand Central Dispatch (threads), animations • Explicit or inline definition • Context variable must be marked with __block if changed within block

Slide 45

Slide 45 text

self.square.backgroundColor = [UIColor redColor]; ... - (void)animate:(id)sender { [UIView animateWithDuration:3.0 animations:^{ self.square.backgroundColor = [UIColor greenColor]; }]; } Blocks

Slide 46

Slide 46 text

Demo github.com/fbernardo/fct_ios_workshop/releases

Slide 47

Slide 47 text

Thank you @fbbernardo @PragmaPilot