14. Controller
#import <UIKit/UIKit.h>
@interface FOViewController : UIViewController
// Model
@property (nonatomic, copy) NSArray *fortunes;
// Outlets
@property (nonatomic, strong) IBOutlet UITextView *fortuneField;
// Actions
- (IBAction)showFortune:(id)sender;
@end
words allow
(IBOutlet & IBAction key
ilder)
connections in Interface Bu
FOViewController.h
15. Controller
#import "FOViewController.h"
@implementation FOViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_fortunes = @[
@"A dubious friend may be an enemy in camouflage.",
@"A feather in the hand is better than a bird in the air.",
@"A friend asks only for your time not your money.",
// ...
];
}
return self;
}
- (IBAction)showFortune:(id)sender {
int fortuneIndex = arc4random() % [_fortunes count];
NSLog(@"fortuneIndex: %d", fortuneIndex);
NSString *fortune = [_fortunes objectAtIndex:fortuneIndex];
[_fortuneField setText:fortune];
}
@end
FOViewController.m
18. Run It!
1. Load view controller
in App Delegate
2. Tap “Get Fortune!”
3. showFortune: called
4. Get fortune from model
5. Call setText: on UITextView
19. Objective-C
Object-oriented
(layered on top of C)
Single-inheritance
Protocols (like Java interfaces)
Message sending
29. UIKit
“The UIKit framework provides the classes needed to
construct and manage an application’s user interface for iOS.
It provides an application object, event handling, drawing model,
windows, views, and controls specifically designed for a touch
screen interface.”
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/_index.html
30. UIKit & Grand Central Dispatch
"For the most part, UIKit classes should be used only from an
application’s main thread. This is particularly true for classes
derived from UIResponder or that involve manipulating your
application’s user interface in any way."
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/
Introduction/Introduction.html#//apple_ref/doc/uid/TP40006955-CH1-SW1
31. Update UI using GCD
-(void)handleIncoming:(NSData *)data
{
dispatch_async(dispatch_get_main_queue(), ^{
// Retrieve data...
// Update the UI using UIKit methods...
});
}
32. Foundation
“The Foundation framework defines a base layer of
Objective-C classes. In addition to providing a set of useful
primitive object classes, it introduces several paradigms that
define functionality not covered by the Objective-C language.”
http://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/ObjC_classic/_index.html
33. Core Graphics
“The Core Graphics framework is a C-based API that is
based on the Quartz advanced drawing engine. It provides low-
level, lightweight 2D rendering with unmatched output fidelity.
You use this framework to handle path-based drawing,
transformations, color management, offscreen rendering,
patterns, gradients and shadings, image data management,
image creation, masking, and PDF document creation, display,
and parsing.”
http://developer.apple.com/library/ios/#documentation/coregraphics/reference/coregraphics_framework/_index.html
34. Many, Many More APIs
Core Location MapKit
Quartz Core Animation
Core Motion AdSupport...
35. Sample Application - Wishlist *
Displays list of items
Move and delete items in list
Detail view to create/edit items
Choose images for items
* http://github.com/sleberknight/wishlist-ios-app
41. Views
UITableView (presents list of items)
* Not defined in XIB
* App delegate creates table view
controller programmatically within
a navigation controller
* Edit and + buttons defined in code
47. WLAppDelegate
application:didFinishLaunchingWithOptions
- Create a WLItemsViewController
- Create a UINavigationController
- Embed WLItemsViewController in
UINavigationController
WLItemsViewController
viewWillAppear: loads table view
List editing UI is automatic when
add button using editBarButton
Tap + button, fires addNewItem: which
presents detail view controller
48. WLDetailViewController init: creates
Cancel & Done bar button items
viewDidLoad: sets background color
viewWillAppear:animated:
- Sets up form for editing/adding item
- Display image if present
- Register for text change notifications
done: dismisses controller,
completion handler reloads table data
viewWillDisappear: saves item data
49. Tapping camera button
invokes takePicture:
UIImagePickerController
presents camera or picture
library
UIImagePickerController
delegate (controller) handles
selected image