Beginning iOS                       Development                             http://bobmccune.comMonday, March 19, 12
Agenda                Beginning iOS Development                       • Understanding the Tools                       • Ob...
iOS SDK ToolsMonday, March 19, 12
XcodeMonday, March 19, 12
Xcode              • Apple’s IDE for creating Mac and iOS apps              • Provides visual front end to LLVM and LLDB  ...
iOS SimulatorMonday, March 19, 12
iOS Simulator              • Device simulator, runs on your desktop                 • Both iPhone and iPad (retina, non-re...
InstrumentsMonday, March 19, 12
Instruments             • Dynamic tracing and profiling tool             • Uses digital audio workstation-like interface   ...
Hands-On TourMonday, March 19, 12
Objective-CMonday, March 19, 12
Agenda                Objective-C Essentials                       •   Overview of Objective-C                       •   U...
Objective-C                The old, new hotness                  • Strict superset of ANSI C                       • Objec...
Creating ClassesMonday, March 19, 12
Creating Classes                The Blueprint                       • Objective-C classes are separated into an           ...
Defining the Interface                 @interface BankAccount : NSObject {                 	 float accountBalance;         ...
Defining the Interface                 @interface BankAccount : NSObject {                 	 float accountBalance;         ...
Defining the Interface                 @interface BankAccount : NSObject {                 	 float accountBalance;         ...
Defining the Interface                 @interface BankAccount : NSObject {                 	 float accountBalance;         ...
Defining the Interface                 @interface BankAccount : NSObject {                 	 float accountBalance;         ...
Defining the Interface                 @interface BankAccount : NSObject {                 	 float accountBalance;         ...
Defining the Implementation                   #import "BankAccount.h"                   @implementation BankAccount        ...
Defining the Implementation                   #import "BankAccount.h"                   @implementation BankAccount        ...
Defining the Implementation                   #import "BankAccount.h"                   @implementation BankAccount        ...
Defining the Implementation                   #import "BankAccount.h"                   @implementation BankAccount        ...
Defining Methods                Understanding the Syntax                       • Class methods                         • Pr...
Defining Methods                Understanding the Syntax                       • Class methods                         • Pr...
Defining Methods                Understanding the Syntax                       • Class methods                         • Pr...
self and super                Talking to yourself                       • Methods have implicit reference to owning       ...
Working with ObjectsMonday, March 19, 12
Two-Stage Creation                • NSObject defines class method called alloc                       • Dynamically allocate...
Messages                Communicating with Objects                       • Methods are invoked by passing messages        ...
Messaging an Object                NSMutableDictionary *person =                     [NSMutableDictionary dictionary];    ...
Declared PropertiesMonday, March 19, 12
Declared Properties                Simplifying Accessors                  • Most common methods we write are accessors    ...
Declaring Properties               @property (attributes) type variable;                         Attribute      Impacts   ...
Declaring Properties               @property (attributes) type variable;                         Attribute          Impact...
Declaring Properties               @property (attributes) type variable;                         Attribute      Impacts   ...
Declaring Properties               @property (attributes) type variable;                         Attribute      Impacts   ...
Declaring Properties               @property (attributes) type variable;                         Attribute        Impacts ...
Declaring Properties                Defining the Interface                @interface BankAccount : NSObject                ...
Synthesizing Properties                Generating the Accessors                @implementation BankAccount                ...
Accessing Properties                       • Generated properties are standard methods                       • Accessed th...
ProtocolsMonday, March 19, 12
Protocols                Object-Oriented Contracts                       • Abstract methods to be implemented             ...
Defining a Protocol                NSCoding                * NSCoding defined in Foundation Framework                @protoc...
Adopting a Protocol                Conforming to the Contract                @interface User : NSObject <NSCoding>        ...
Adopting a Protocol                Conforming to the Contract                @interface User : NSObject <NSCoding>        ...
Adopting a Protocol                 Conforming to the Contract                 @implementation User                 -(id)i...
Time to Code!Monday, March 19, 12
Using Cocoa TouchMonday, March 19, 12
Cocoa Touch               Foundation Framework                   • Core framework for non-UI functionality                ...
Cocoa Touch               UIKit Framework                   • Framework for building iOS user interfaces                  ...
Understanding MVC                Model,View, Controller                       Model                  View                 ...
UIView                The “V” in MVC                       • Base class for iOS user interface components                 ...
UIViewController                The “C” in MVC                • The heart of every iOS app                • Provides the “...
UIViewController: View Loading           controller.view             View Controller                              Is view ...
View Unloading                Dealing with low memory conditions                       • didReceiveMemoryWarning          ...
Display-related Callbacks                       viewWillAppear                        • Called immediately prior to being ...
Outlets and Actions                Wiring the View and View Controller                       • Provides metadata to Interf...
Time to Code!Monday, March 19, 12
Upcoming SlideShare
Loading in …5
×

Beginningi os part1-bobmccune

1,001 views
890 views

Published on

Mobile March 2012

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,001
On SlideShare
0
From Embeds
0
Number of Embeds
275
Actions
Shares
0
Downloads
43
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Beginningi os part1-bobmccune

  1. 1. Beginning iOS Development http://bobmccune.comMonday, March 19, 12
  2. 2. Agenda Beginning iOS Development • Understanding the Tools • Objective-C Crash Course • Using Cocoa TouchMonday, March 19, 12
  3. 3. iOS SDK ToolsMonday, March 19, 12
  4. 4. XcodeMonday, March 19, 12
  5. 5. Xcode • Apple’s IDE for creating Mac and iOS apps • Provides visual front end to LLVM and LLDB • Hub of development process: Editing UI Design Building Testing Refactoring Model Design Debugging Deployment API Doc Integration Project ConfigurationMonday, March 19, 12
  6. 6. iOS SimulatorMonday, March 19, 12
  7. 7. iOS Simulator • Device simulator, runs on your desktop • Both iPhone and iPad (retina, non-retina) • Faster code, build, test cycle • Test behaviors: rotation, shake, multi-touch • Easier testing of exceptional conditions • iOS Simulator != iOS Device: • No memory or CPU limits • Not all APIs and capabilities availableMonday, March 19, 12
  8. 8. InstrumentsMonday, March 19, 12
  9. 9. Instruments • Dynamic tracing and profiling tool • Uses digital audio workstation-like interface • Large library of standard instruments • Core Animation, Leaks, Automation, etc. • The ultra geeky can build custom instrumentsMonday, March 19, 12
  10. 10. Hands-On TourMonday, March 19, 12
  11. 11. Objective-CMonday, March 19, 12
  12. 12. Agenda Objective-C Essentials • Overview of Objective-C • Understanding Classes and Objects • Methods, Messages, and Properties • ProtocolsMonday, March 19, 12
  13. 13. Objective-C The old, new hotness • Strict superset of ANSI C • Object-oriented extensions • Additional syntax and types • Dynamic, Object-Oriented Language: • Dynamic Typing • Dynamic Binding • Dynamic LoadingMonday, March 19, 12
  14. 14. Creating ClassesMonday, March 19, 12
  15. 15. Creating Classes The Blueprint • Objective-C classes are separated into an interface and an implementation. • Usually defined in separate .h and .m files •Defines the •Defines the actual programming implementation interface code •Can define object instance •Implement one or variables more initializers to properly initialize an object instanceMonday, March 19, 12
  16. 16. Defining the Interface @interface BankAccount : NSObject { float accountBalance; NSString *accountNumber; } - (float)withDraw:(float)amount; - (void)deposit:(float)amount; @endMonday, March 19, 12
  17. 17. Defining the Interface @interface BankAccount : NSObject { float accountBalance; NSString *accountNumber; } - (float)withDraw:(float)amount; - (void)deposit:(float)amount; @endMonday, March 19, 12
  18. 18. Defining the Interface @interface BankAccount : NSObject { float accountBalance; NSString *accountNumber; } - (float)withDraw:(float)amount; - (void)deposit:(float)amount; @endMonday, March 19, 12
  19. 19. Defining the Interface @interface BankAccount : NSObject { float accountBalance; NSString *accountNumber; } - (float)withDraw:(float)amount; - (void)deposit:(float)amount; @endMonday, March 19, 12
  20. 20. Defining the Interface @interface BankAccount : NSObject { float accountBalance; NSString *accountNumber; } - (float)withDraw:(float)amount; - (void)deposit:(float)amount; @endMonday, March 19, 12
  21. 21. Defining the Interface @interface BankAccount : NSObject { float accountBalance; NSString *accountNumber; } - (float)withDraw:(float)amount; - (void)deposit:(float)amount; @endMonday, March 19, 12
  22. 22. Defining the Implementation #import "BankAccount.h" @implementation BankAccount - (id)init { self = [super init]; return self; } - (float)withdraw:(float)amount { // calculate valid withdrawal return amount; } - (void)deposit:(float)amount { // record transaction } @endMonday, March 19, 12
  23. 23. Defining the Implementation #import "BankAccount.h" @implementation BankAccount - (id)init { self = [super init]; return self; } - (float)withdraw:(float)amount { // calculate valid withdrawal return amount; } - (void)deposit:(float)amount { // record transaction } @endMonday, March 19, 12
  24. 24. Defining the Implementation #import "BankAccount.h" @implementation BankAccount - (id)init { self = [super init]; return self; } - (float)withdraw:(float)amount { // calculate valid withdrawal return amount; } - (void)deposit:(float)amount { // record transaction } @endMonday, March 19, 12
  25. 25. Defining the Implementation #import "BankAccount.h" @implementation BankAccount - (id)init { self = [super init]; return self; } - (float)withdraw:(float)amount { // calculate valid withdrawal return amount; } - (void)deposit:(float)amount { // record transaction } @endMonday, March 19, 12
  26. 26. Defining Methods Understanding the Syntax • Class methods • Prefixed with a + • Tied to class, not instance • Instance Methods • Instance methods prefix with a - • Modifies state of an object instance - (NSString *)description;Monday, March 19, 12
  27. 27. Defining Methods Understanding the Syntax • Class methods • Prefixed with a + • Tied to class, not instance • Instance Methods • Instance methods prefix with a - • Modifies state of an object instance - (BOOL)respondsToSelector:(SEL)aSelector;Monday, March 19, 12
  28. 28. Defining Methods Understanding the Syntax • Class methods • Prefixed with a + • Tied to class, not instance • Instance Methods • Instance methods prefix with a - • Modifies state of an object instance + (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;Monday, March 19, 12
  29. 29. self and super Talking to yourself • Methods have implicit reference to owning object called self • Additionally have access to superclass methods using super -(id)init { self = [super init]; if (self) { // do initialization } return self; }Monday, March 19, 12
  30. 30. Working with ObjectsMonday, March 19, 12
  31. 31. Two-Stage Creation • NSObject defines class method called alloc • Dynamically allocates memory for object • Returns new instance of receiving class BankAccount *account = [BankAccount alloc]; • NSObject defines instance method init • Implemented by subclasses to initialize instance after memory for it has been allocated • Subclasses commonly define several initializers account = [account init]; • alloc and init calls should always nested into single line BankAccount *account = [[BankAccount alloc] init];Monday, March 19, 12
  32. 32. Messages Communicating with Objects • Methods are invoked by passing messages • Methods are never directly invoked • Messages dynamically bound to method implementations at runtime • Simple messages take the form: • [object message]; • Can pass one or more arguments: • [object methodWithArg1:arg1 arg2:arg2];Monday, March 19, 12
  33. 33. Messaging an Object NSMutableDictionary *person = [NSMutableDictionary dictionary]; [person setObject:@"Joe Smith" forKey:@"name"]; NSString *address = @"123 Street"; NSString *house = [address substringWithRange:NSMakeRange(0, 3)]; [person setObject:house forKey:@"houseNumber"]; NSArray *children = [NSArray arrayWithObjects:@"Jack", @"Susie", nil]; [person setObject:children forKey:@"children"];Monday, March 19, 12
  34. 34. Declared PropertiesMonday, March 19, 12
  35. 35. Declared Properties Simplifying Accessors • Most common methods we write are accessors • Objective-C can automatically generate accessors • Much less verbose, less error prone • Highly configurable • Declared properties are defined in two parts: • @property definition in the interface • @synthesize statement in the implementationMonday, March 19, 12
  36. 36. Declaring Properties @property (attributes) type variable; Attribute Impacts nonatomic Concurrency readonly/readwrite Mutability strong/copy/ Storage weak/assign getter/setter APIMonday, March 19, 12
  37. 37. Declaring Properties @property (attributes) type variable; Attribute Impacts nonatomic Concurrency readonly/readwrite Mutability strong/copy/ Storage weak/assign getter/setter API @property (readonly) BOOL active;Monday, March 19, 12
  38. 38. Declaring Properties @property (attributes) type variable; Attribute Impacts nonatomic Concurrency readonly/readwrite Mutability strong/copy/ Storage weak/assign getter/setter API @property (nonatomic, readonly) BOOL active;Monday, March 19, 12
  39. 39. Declaring Properties @property (attributes) type variable; Attribute Impacts nonatomic Concurrency readonly/readwrite Mutability strong/copy/ Storage weak/assign getter/setter API @property (nonatomic, strong) NSDate *activeDate;Monday, March 19, 12
  40. 40. Declaring Properties @property (attributes) type variable; Attribute Impacts nonatomic Concurrency readonly/readwrite Mutability strong/copy/ Storage weak/assign getter/setter API @property (readonly, getter=isValid) BOOL valid;Monday, March 19, 12
  41. 41. Declaring Properties Defining the Interface @interface BankAccount : NSObject @property (nonatomic, copy) NSString *accountNumber; @property (nonatomic, strong) NSDecimalNumber *balance; @property (readonly, strong) NSDecimalNumber *fees; @property (getter=isCurrent) BOOL accountCurrent; @endMonday, March 19, 12
  42. 42. Synthesizing Properties Generating the Accessors @implementation BankAccount @synthesize accountNumber = _accountNumber; @synthesize balance = _balance; @synthesize fees = _fees; @synthesize accountCurrent = _accountCurrent; @endMonday, March 19, 12
  43. 43. Accessing Properties • Generated properties are standard methods • Accessed through normal messaging syntax [object property]; [object setProperty:newValue]; • Objective-C property access via dot notation object.property; object.property = newValue; Dot notation is just syntactic sugar. Still uses accessor methods. Doesnt get/set values directly.Monday, March 19, 12
  44. 44. ProtocolsMonday, March 19, 12
  45. 45. Protocols Object-Oriented Contracts • Abstract methods to be implemented • Methods are not tied to a specific class • Analogous to C# and Java interfaces • Useful in defining: • Capturing similarities among classes that arent hierarchically related • Declaring an interface while hiding its particular class • Delegate callback methodsMonday, March 19, 12
  46. 46. Defining a Protocol NSCoding * NSCoding defined in Foundation Framework @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; - (id)initWithCoder:(NSCoder *)aDecoder; @endMonday, March 19, 12
  47. 47. Adopting a Protocol Conforming to the Contract @interface User : NSObject <NSCoding> @property (nonatomic, copy) NSString *username; @property (nonatomic, copy) NSString *password; @endMonday, March 19, 12
  48. 48. Adopting a Protocol Conforming to the Contract @interface User : NSObject <NSCoding> @property (nonatomic, copy) NSString *username; @property (nonatomic, copy) NSString *password; @endMonday, March 19, 12
  49. 49. Adopting a Protocol Conforming to the Contract @implementation User -(id)initWithCoder:(NSCoder *)coder { if (self = [super init]) { self.username = [coder decodeObjectForKey:@"username"]; self.password = [coder decodeObjectForKey:@"password"]; } return self; } -(void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:self.username forKey:@"username"]; [coder encodeObject:self.username forKey:@"password"]; } @endMonday, March 19, 12
  50. 50. Time to Code!Monday, March 19, 12
  51. 51. Using Cocoa TouchMonday, March 19, 12
  52. 52. Cocoa Touch Foundation Framework • Core framework for non-UI functionality • Operating System Classes • threading, archiving, filesystem • Collections • Common collection types: arrays, dictionaries, sets • Networking support • XML ProcessingMonday, March 19, 12
  53. 53. Cocoa Touch UIKit Framework • Framework for building iOS user interfaces • User Interface Elements • Tables, buttons, images, etc. • Views and View Controllers • Multitouch Event Handling • High-level Drawing RoutinesMonday, March 19, 12
  54. 54. Understanding MVC Model,View, Controller Model View ControllerMonday, March 19, 12
  55. 55. UIView The “V” in MVC • Base class for iOS user interface components • Drawing and animation • Layout and subview management • Multitouch event handling • Xcode’s Interface Builder used to build UI • Archived version of UI stored in XIB/NIB file • Dynamically loaded at runtime by controller • Can be created programmaticallyMonday, March 19, 12
  56. 56. UIViewController The “C” in MVC • The heart of every iOS app • Provides the “glue code” between the model and view • Responsible for managing the view hierarchy and lifecycle • Key view lifecycle methods: - (void)loadView; - (void)viewDidLoad; - (void)viewDidUnload;Monday, March 19, 12
  57. 57. UIViewController: View Loading controller.view View Controller Is view nil? View loadView viewDidLoad 1) Overridden 2) Loaded from NIB 3) Return empty UIViewMonday, March 19, 12
  58. 58. View Unloading Dealing with low memory conditions • didReceiveMemoryWarning • Called by OS when low memory encountered • If possible, will release controller’s view • Can override, but must call super • viewDidUnload invoked if root view released • Override to release anything that can be recreated in loadView/viewDidLoadMonday, March 19, 12
  59. 59. Display-related Callbacks viewWillAppear • Called immediately prior to being presented viewDidAppear • Called upon view being added to window viewWillDisappear • Called immediately before view is removed from window or is covered by another view viewDidDisappear • Called after view is removed from window or is covered by another viewMonday, March 19, 12
  60. 60. Outlets and Actions Wiring the View and View Controller • Provides metadata to Interface Builder to allow connections between objects • IBOutlet • Property reference to XIB/NIB objects • Null-defined C macro • IBAction • Used by Interface Builder to determine available actions • Void-defined C macroMonday, March 19, 12
  61. 61. Time to Code!Monday, March 19, 12

×