Post WDI - iPhone MVC App Design Approach Fundamentals
iPhone Development (Xcode 5) (Cont'd)
- MVC App Design Approach with iPhone { DONE
- Definition: Model-View-Controller (Controller bridges components, Model supplies Data, View obtains and reacts to user input)
- Benefits: Reusable Object-Oriented Programs, Multiple Developer Teams (i.e. Git) ('Separation of Concerns' between functional units), Maintainable & Extendability of App
- Typical App Req'ments: User Interface, Handle & React to User Input, Store Info to React Correctly
- Views (handle the UI)
- Connection Points (Outlets, Actions) required for objects in View to interact with app logic. Define Outlets and Actions in Controller (where code implements logic of Views)
- Outlets Code-to-View path to read/write values
- Actions Method triggered by event in View
- View Controllers (implement functional logic)
- Use:
- Define Outlets for Connection Points (IBOutlet Directive in .h)
- Define Actions for Connection Points (IBAction Directive in .h)
- Handles interactions with View
- Note: IBOutlet and IBAction recognised as markers by IB
- Example with @property and @synthesise
// declare instance 'myLabel'
@property (retain, nonatomic) NSString *myLabel;
// simplify accessing properties defined in header file using @synthesize in the implementation file to create getters/setters conveniently.
@synthesize myLabel;
// get/set current value from UILabel 'text'
propertycurrentLabel = myLabel.text // getter
myLabel.text = @"My New Label" // setter (noting that UILabel and UITextField have a property called 'text')
- Example of instance method declaration using adaptable 'id' generic data type
// declare instance method used as action.
// accepts 'id' special generic data type object as parameter
// for variable that causes input param availability
-(IBAction)doCalculation:(id)sender;
- Data Models
- In-Controller Data Models
- Standalone Separate Data Models (Core Data modelling tool from iPhone SDK to visually lay out interfaces and map a data structure)
- View-Base Application Template Practice (without Core Data modelling tools)
- Classes
- Example (__AppDelegate.m)
// define instance method. action is adding view to window and make it visible
// accepts UIApplication (object class that handles events) variable as parameter
// 'application' makes input parameter available
-(void)applicationDidFinishLaunching:(UIApplication *)application {
// override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- ViewController Files __ViewController.h and __ViewController.m
- Implements the view controller class (UIViewController) containing logic to control a view
- PList File
- Use: Configure so XIB File loaded when app starts (i.e. MainWindow.xib), which instantiates the ViewController class, whose View is loaded from its associated XIB file
- Example (Create View Controller Code Outlets and Actions as IB Connection Points)
// declare view controller outlets directives and actions in view controller header file to enable IB to visually connect view label object to userOutput instance variable. *userOutput accesses instance variable in view controller
// declare outputs within @interface block
IBOutlet UILabel *userOutput;
IBOutlet UITextField *userInput;
// define variables as properties for easy access (after the @interface block)
@property (retain, nonatomic) UITextField *userInput;
@property (retain, nonatomic) UILabel *userOutput;
// simplify accessing properties defined in header file using @synthesize in the implementation file to create getters/setters conveniently. alternative to the following @synthesize code is less readable and maintainable [userOutput setText: [userInput getText]];
@synthesize userOutput;
@synthesize userInput;
// declare instance method in the implementation file of the view controller used as an action (explained in previous example)
// get the value of UILabel's property called 'text' for variable 'userInput'
// set it to the value of UILabel's property called 'text' for variable 'userOutput'
-(IBAction)setOutput:(id)sender { userOutput.text=userInput.text;}
- Designing User Interface (UI) View with Interface Builder (IB)
- Connecting the View to the View Controller Outlets and Actions with IB
- Connection Inspector (instead of drag-drop) for connecting event objects (i.e. Buttons)
- Memory Release
// use 'release' method in the dealloc method of view controller[userInput release];[userOutput release];[super dealloc];
No comments:
Post a Comment