Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPhone 101. Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode.

Similar presentations


Presentation on theme: "IPhone 101. Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode."— Presentation transcript:

1 iPhone 101

2 Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode

3 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)

4 Objective-C File extensions:.h,.m, and.mm Class definition: @interface StringParser : NSObject { @private NSString* fContent; } - (id)init:(NSString *)content options:(int)options; + (int)SomeStaticMethod:(int)value; @end

5 Objective-C #import “StringParser.h” @implementation StringParser - (id)init:(NSString *)content options:(int)options { fContent = [content retain]; return self; } - (void)dealloc { [fContent release]; [super dealloc]; } + (int)SomeStaticMethod:(int)value { } @end

6 More Objective-C Object allocation, destruction and reference counting using retain/release/autorelease Categories Properties Delegate objects using @protocols

7 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];

8 More Objective-C (categories) /* declaring categories */ @interface NSString (NSString_URLEncode) - (NSString *) encodeAsURL; @end @implementation NSString (NSString_URLEncode) - (NSString *) encodeAsURL { /* do stuff using public methods of NSString */ } @end /* using the category */ NSString *foo = SomeFunctionThatReturnsAString(); [request open:@”GET” url:[foo encodeAsURL]];

9 More Objective-C (properties) /* in class definition */ @private NSObject *delegate; NSString *fName; /* in method area (after) */ @property(assign) NSObject * delegate; @property(nonatomic, retain) name; /* implementation */ @synthesize delegate; @synthesize name = fName; /* accessing */ Foo *foo = [[foo alloc] init]; foo.delegate = self; foo.name = @”bar”;

10 More Objective-C (delegates) /* protocol declaration */ @protocol RequestManagerDelegate - (void)onLoginComplete:(YYLoginStatus)status msg:(NSString *)msg; @end /* class which has a delegate object */ NSObject *delegate; /* Calling a delegate method */ [delegate onLoginComplete:kNetworkError msg:nil]; /* class that implements the delegate methods */ @interface SomeClass : BaseClass @end /* delegate method implementation */ #pragma mark RequestManager delegate methods - (void)onLoginComplete:(YYLoginStatus)status msg:(NSString *)msg { }

11 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 *Note: @synchronized (obj) can be used by multi-threaded apps

12 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: syntax: @selector(methodName:arg1:arg2:) Implement delegate protocols to handle events from various UI/data objects

13 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

14 Anatomy of an iPhone app UINavigationBar / Item UIBarButtonItem UIImageView / UIImage UILabel UITextField UIButton UITabBarController UITabBarItem/badgeValue

15 Tab bar components

16 Navigation item components

17 UICatalog sample app

18 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

19 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


Download ppt "IPhone 101. Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode."

Similar presentations


Ads by Google