IPhone 101. Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode.
-
date post
15-Jan-2016 -
Category
Documents
-
view
222 -
download
0
Transcript of IPhone 101. Outline Objective-C Random bits of the API Using the simulator Debugging with Xcode.
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 definition:
@interface StringParser : NSObject {@private
NSString* fContent;}
- (id)init:(NSString *)content options:(int)options;+ (int)SomeStaticMethod:(int)value;
@end
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
More Objective-C
Object allocation, destruction and reference counting using retain/release/autorelease
Categories Properties Delegate objects using @protocols
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 */@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]];
More Objective-C (properties)/* in class definition */@private
NSObject<RequestManagerDelegate> *delegate;NSString *fName;
/* in method area (after) */@property(assign) NSObject<RequestManagerDelegate>* delegate;@property(nonatomic, retain) name;
/* implementation */@synthesize delegate;@synthesize name = fName;
/* accessing */Foo *foo = [[foo alloc] init];foo.delegate = self;foo.name = @”bar”;
More Objective-C (delegates)/* protocol declaration */@protocol RequestManagerDelegate- (void)onLoginComplete:(YYLoginStatus)status msg:(NSString *)msg;@end
/* class which has a delegate object */NSObject<RequestManagerDelegate> *delegate;
/* Calling a delegate method */[delegate onLoginComplete:kNetworkError msg:nil];
/* class that implements the delegate methods */@interface SomeClass : BaseClass <RequestManagerDelegate>@end
/* 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*Note: @synchronized (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: syntax:
@selector(methodName:arg1:arg2:) 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