cs 696 mobile application development fall semester, 2011 ...first example 3 counter + increases...
TRANSCRIPT
-
CS 696 Mobile Application DevelopmentFall Semester, 2011
Doc 7 iPhone App IntroSep 15, 2011
Copyright ©, All rights reserved. 2011 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.
Thursday, September 15, 2011
-
References
2
View Controller Programming Guide for iOS
iPhone Application Programming Guide
Various UIKit class's documentation
CS 193P Standford iPhone Application Development, Lecture 4, http://itunes.apple.com/us/podcast/lecture-4-slides-january-14/id384233225?i=85092607
Thursday, September 15, 2011
-
First Example
3
Counter+ increases count- decrease count
Thursday, September 15, 2011
-
Things to Master
4
Concepts Frameworks
Application & Delegation
Model View Contoller
Events
UIKitClasses, methodsHow it works
Tools
XcodeInterface builderxib filesresources etc
Thursday, September 15, 2011
-
MVC - Model View Controller
5
ModelApp data & functionality
ViewThe user interface
ControllerIntermediary between Model & ViewWhen model changes updates viewWhen user interacts with view updates modelApp logic lives here (sigh)
Thursday, September 15, 2011
-
App Code
6
View
Controller
Model
Thursday, September 15, 2011
-
UIApplication
7
All iOS apps are instances of UIApplication class
Handles lifecycle of the application
Handles events from OS
Manages status bar
Contains all applicaition windows
Thursday, September 15, 2011
-
OS Events & Your App
8
Some OS events have to be handled by your code
App startedApp moving to backgroundApp terminatingApp received memory warningApp received notification
Thursday, September 15, 2011
-
How does your code get the events?
9
UIApplication
AppSubclassA AppSubclassB
Common Java pattern - create subclass
But iOS does not use this pattern
Thursday, September 15, 2011
-
Delegate
10
entrust (a task or responsibility) to another person
UIApplicationDelegate
View
Controller
Model
OS
Your Code
Thursday, September 15, 2011
-
Delegate Pattern
11
Common pattern in iOS framework
Framework class in charge of some task
Parts of task must be done by your code
Rather than subclass framework class
Framework class uses a delegate class
Uses delegate to perform some of its tasks
We implement a delegate class
Thursday, September 15, 2011
-
Counter Project
12
Click on + Calls increasePressed method on controllerController has view update value
Click on -Calls increasePressed method on controllerController has view update value
+ & - button
2label that changes with count
Thursday, September 15, 2011
-
Starting new Project - Xcode 4
13
Thursday, September 15, 2011
-
Starting new Project - Xcode 4
14
Thursday, September 15, 2011
-
Files Created For Counter Project
15
ClassesCounterAppDelegate.hCounterAppDelegate.mCounterViewController.hCounterViewController.m
Other SourcesCounter_Prefix.pchmain.mCounterViewController.xibMainWindow.xibCounter-Info.plist
(Where is the model?)
Thursday, September 15, 2011
-
main.m
16
#import
int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal;}
Thursday, September 15, 2011
-
Counter_Prefix.pch
17
#ifdef __OBJC__ #import #import #endif
Thursday, September 15, 2011
-
Counter-Info.plist
18
plist - property list
Thursday, September 15, 2011
-
CounterViewController.h
19
#import
@interface CounterViewController : UIViewController {
}
@end
Thursday, September 15, 2011
We will add to this class
-
CounterAppDelegate
20
#import
@class CounterViewController;
@interface CounterAppDelegate : NSObject { UIWindow *window; CoutnerViewController *viewController;}
@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) IBOutlet CounterViewController *viewController;
@end
Thursday, September 15, 2011
We will deal this class for a while
-
Creating the view
21
Use Interface Builder to draw view
or
Create view in code
Thursday, September 15, 2011
-
Views with Interface Builder
22
Draw the view in Interface Builder
Save the view (xib file)
Declare connections in code
Make connection in Interface Builder
Thursday, September 15, 2011
-
View
23
xibXML file containing UI layout for a screenGenerated by Interface Builder
nibbinary version of xib used in compiled app
Normally defined in xib & nib files
Thursday, September 15, 2011
-
Interface Builder - Xcode 4
24
Thursday, September 15, 2011
-
Interface Builder - Xcode 3
25
Thursday, September 15, 2011
-
Declaring connections in Code
26
@interface CounterViewController : UIViewController { int * count;}@property (nonatomic, retain) IBOutlet UILabel * countDisplay;
- (IBAction) increasePressed;- (IBAction) decreasePressed;@end
Controller
+IBActions
IBOutlets-
2
View
increasedPresseddecreasedPressed
countDisplay
Thursday, September 15, 2011
-
Making Connections - Xcode 4
27
Thursday, September 15, 2011
You need to right-click on the button and drag to the Controller.h file to add a new Action/Outlet. If you don't have a 2 button mouse you need to control-click.
-
Connecting to Existing Action/Outlet
28
Thursday, September 15, 2011
Right-click (or control click) on button and drag to the file's Owner to connect to an existing Action/outlet
-
Making the Connection in Interface Builder
29
Thursday, September 15, 2011
-
The Connections
30
IBActionMethod in controllerConnect UI event to methodUI element triggers methodTypedef - is void
IBOutletInstance variable in controllerReference ti UI elementEmpty typedef
Thursday, September 15, 2011
-
31
Demo
Thursday, September 15, 2011
-
32
CounterViewController.h
#import
@interface CounterViewController : UIViewController { int count;}@property (nonatomic, retain) IBOutlet UILabel * countDisplay;
- (IBAction) increasePressed;- (IBAction) decreasePressed;
@end
Thursday, September 15, 2011
-
CounterViewController.h
33
#import "CounterViewController.h"@implementation CounterViewController@synthesize countDisplay;
- (void) displayCount { [countDisplay setText:[NSString stringWithFormat:@"%i", count]];}
- (IBAction) increasePressed { count++; [self displayCount];}
- (IBAction) decreasePressed { count--; [self displayCount];}
- (void)dealloc { [super dealloc]; [countDisplay release];}@end
Thursday, September 15, 2011
-
CounterViewController
34
No changes made to generated code
Thursday, September 15, 2011
-
Action Method options
35
- (void)actionMethod; - (void)actionMethod:(id)sender; - (void)actionMethod:(id)sender withEvent:(UIEvent *)event;
Thursday, September 15, 2011
-
- (void)actionMethod;
36
When no data is needed from UI element
- (IBAction) increasePressed { count++; [self displayCount];}
Thursday, September 15, 2011
-
- (void)actionMethod:(id)sender;
37
When need data from senderWhen need to interact with sender
- (IBAction) increasePressed: (id) sender { UIButton * buttonPressed = (UIButton*) sender; if ([buttonPressed.titleLabel.text isEqualToString: @"+"]) { count++; [self displayCount]; if (count > 5) { [buttonPressed setTitle: @"X" forState: UIControlStateNormal]; } }}
Thursday, September 15, 2011
-
- (void)actionMethod:(id)sender withEvent:(UIEvent *)event;
38
- (IBAction) increasePressed: (id) sender withEvent: (UIEvent *) event { NSSet * touches = [event allTouches]; count = count + [touches count]; [self displayCount];}
Event contains information about the UI event
Thursday, September 15, 2011
-
Where is the Model?
39
View
Controller
Modelint count was model
Used default initial value
Thursday, September 15, 2011
-
Counter Model
40
@interface Counter : NSObject { int count;}
-(void) increase;-(void) decrease;-(int) count;
@end
@implementation Counter
-(void) increase { count++; }
-(void) decrease { count--; }
-(int) count { return count; }
- (id) init { if (self = [super init]) { count = 0; } return self;}@end
Thursday, September 15, 2011
-
CounterViewController.h
41
#import #import "Counter.h"
@interface CounterViewController : UIViewController { Counter * count;}@property (nonatomic, retain) IBOutlet UILabel * countDisplay;
- (IBAction) increasePressed;- (IBAction) decreasePressed;@end
Thursday, September 15, 2011
-
Using the model
42
- (void) displayCount { [countDisplay setText:[NSString stringWithFormat:@"%i", [count count]]];}
- (IBAction) increasePressed { [count increase]; [self displayCount];}
- (IBAction) decreasePressed { [count decrease]; [self displayCount];}
Thursday, September 15, 2011
-
Creating & releasing the Model
43
- (void)dealloc { [super dealloc]; [countDisplay release]; [count release];}
- (void) awakeFromNib { NSLog(@"awakefromNib"); count = [Counter new];}
Thursday, September 15, 2011
-
App life cycle
44
Thursday, September 15, 2011
diagram from "iPhone Application Programming Guide"
-
Starting points for your app code
45
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions
App Delegate
- (id) initWithCoder:(NSCoder *) aDecoder- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil- (void) awakeFromNib- (void) viewDidLoad
Controller
Thursday, September 15, 2011
-
Creating Views in code
46
When count >=3 Create & display "Done"
Thursday, September 15, 2011
-
CounterViewController.h
47
#import #import "Counter.h"
@interface CounterViewController : UIViewController { Counter * count;}@property (nonatomic, retain) IBOutlet UILabel * countDisplay;@property (nonatomic, retain) IBOutlet UILabel * doneLabel;
- (IBAction) increasePressed;- (IBAction) decreasePressed;@end
Thursday, September 15, 2011
-
CounterViewController.m
48
#import "CounterViewController.h"
@implementation CounterViewController
@synthesize countDisplay;@synthesize doneLabel;
- (void) displayCount { [countDisplay setText:[NSString stringWithFormat:@"%i", [count count]]];}
Thursday, September 15, 2011
-
CounterViewController.m
49
- (IBAction) increasePressed { [count increase]; [self displayCount]; if ([count count] >= 3) { [self displayDoneLabel]; }}
- (void) displayDoneLabel { if (doneLabel == nil) { [self createDoneLabel]; } else { doneLabel.hidden = NO; }}
Thursday, September 15, 2011
-
CounterViewController.m
50
- (void) createDoneLabel { CGRect frame = CGRectMake(115, 300, 90, 30); [self setDoneLabel: [[UILabel alloc] initWithFrame:frame]]; doneLabel.textAlignment = UITextAlignmentCenter; doneLabel.font = [UIFont fontWithName:@"Verdana" size:24]; doneLabel.text = @"Done"; [self.view addSubview:doneLabel];}
Thursday, September 15, 2011
-
CounterViewController.m
51
- (IBAction) decreasePressed { [count decrease]; [self displayCount]; if ([count count] < 3) { doneLabel.hidden = YES; }}
Thursday, September 15, 2011