190 likes | 203 Views
Learn the basics of iPhone app development using Objective-C, including working with UI elements, debugging with Xcode, and utilizing the iOS simulator. Explore topics such as inheritance, categories, properties, and delegates. Discover the Cocoa Touch API and its data types, collections, and built-in features. Dive into the anatomy of an iPhone app, including app delegates, navigation and tab bars, view controllers, and event handling. Get hands-on experience with a tab-bar based app and learn about memory management and localization.
E N D
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:
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.
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
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 • Enable code in applicationDidFinishLaunching to add one view controller • In FirstViewController, add a red box at 10,10 which is 100x100 using UIView::initWithFrame and CGRectMake() • Add a white label in that box at 10,10 with a 12pt system font • Add a second view controller and give it a yellow background • Add names and tab bar images (UITabBarSystemItem) to both view controllers • Inspect code for memory leaks. Did you remember to release after adding views to subviews? • 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. • Use Xcode to set breakpoints and inspect variables