iPhone 101
Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode
Objective-C Superset of 'C' with OO stuff added C++ classes → interface/implementation C++ methods → messages No static member variables Only need to declare public methods or those needed before the implementation Inheritance with Java-like multiple inheritance (using protocols)
Objective-C File extensions:.h,.m, and.mm Class StringParser : NSObject NSString* fContent; } - (id)init:(NSString *)content options:(int)options; +
Objective-C #import StringParser - (id)init:(NSString *)content options:(int)options { fContent = [content retain]; return self; } - (void)dealloc { [fContent release]; [super dealloc]; } + (int)SomeStaticMethod:(int)value {
More Objective-C Object allocation, destruction and reference counting using retain/release/autorelease Categories Properties Delegate objects
More Objective-C Most Objective-C methods return the object itself (as an 'id') so you can chain calls in a single statement. /* nested brackets */ bb = [[[UIBarButtonItem alloc] initWithCustomView:logo] autorelease]; NSArray *paths = SomeFunction(); NSString *downloadPath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:kDownloadFolder] retain];
More Objective-C (categories) /* declaring categories NSString (NSString_URLEncode) - (NSString NSString (NSString_URLEncode) - (NSString *) encodeAsURL { /* do stuff using public methods of NSString */ /* using the category */ NSString *foo = SomeFunctionThatReturnsAString(); [request url:[foo encodeAsURL]];
More Objective-C (properties) /* in class definition NSObject *delegate; NSString *fName; /* in method area (after) NSObject * retain) name; /* implementation name = fName; /* accessing */ Foo *foo = [[foo alloc] init]; foo.delegate = self; foo.name
More Objective-C (delegates) /* protocol declaration RequestManagerDelegate - (void)onLoginComplete:(YYLoginStatus)status msg:(NSString /* class which has a delegate object */ NSObject *delegate; /* Calling a delegate method */ [delegate onLoginComplete:kNetworkError msg:nil]; /* class that implements the delegate methods SomeClass : /* delegate method implementation */ #pragma mark RequestManager delegate methods - (void)onLoginComplete:(YYLoginStatus)status msg:(NSString *)msg { }
Cocoa Touch API Data types: id, BOOL, NSInteger, NSUInteger, NSNumber, NSString, long long Collections: NSDictionary, NSArray, NSSet and their mutable friends NSTimer, NSThread*, NSURLConnection UIResponder for managing the responder chain and touch events Core Graphics, Core Animation, Core Audio, Core Location (obj) can be used by multi-threaded apps
Anatomy of an iPhone app App delegate: lifecycle (ex: applicationDidFinishLaunching, applicationWillTerminate) Navigation bar, tab bar, and view controllers Event driven using a “run loop” (provided for main thread) Register “selectors” to handle events: Implement delegate protocols to handle events from various UI/data objects
Anatomy of an iPhone app UI construction – code or Interface builder UIKit is the iPhone UI framework UIViewController for managing screens UI elements: UIView, UIImageView, UIButton, UIPickerView, UILabel, UITextView, UITableView, UIAlertView, etc. Some UIView useful methods: addSubview and removeFromSuperview beginAnimations:context: method and transform property for simple implicit animation
Anatomy of an iPhone app UINavigationBar / Item UIBarButtonItem UIImageView / UIImage UILabel UITextField UIButton UITabBarController UITabBarItem/badgeValue
Tab bar components
Navigation item components
UICatalog sample app
iPhone SDK miscellany retain/release/autorelease carefully! SDK class factory functions usually return autorelease'd objects (ex: [NSString stringWithFormat...]) A view-controller's loadView can get called multiple times if the view is released due to low memory Localizing strings using NSLocalizedString User-defined “OTHER_CFLAGS” for build flags
Workshop - A simple tab-bar based iPhone app 1.Enable code in applicationDidFinishLaunching to add one view controller 2.In FirstViewController, add a red box at 10,10 which is 100x100 using UIView::initWithFrame and CGRectMake() 3.Add a white label in that box at 10,10 with a 12pt system font 4.Add a second view controller and give it a yellow background 5.Add names and tab bar images (UITabBarSystemItem) to both view controllers 6.Inspect code for memory leaks. Did you remember to release after adding views to subviews? 7.In the second view controller, use a timer (NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:) with 1 second interval to increment a counter (an instance of UILabel) somewhere in the middle of the screen. Use NSString::stringWithFormat to convert a number to a string printf-style. 8.Use Xcode to set breakpoints and inspect variables