290 likes | 304 Views
EEC-492/693/793 iPhone Application Development. Lecture 3 Wenbing Zhao & Nigamanth Sridhar. Outline. Objective-C Property Model-View-Controller A primer on Objective-Oriented Design Building the Button_Fun app. Declared Properties.
E N D
EEC-492/693/793iPhone Application Development Lecture 3 Wenbing Zhao & Nigamanth Sridhar EEC492/693/793 - iPhone Application Development
Outline Objective-C Property Model-View-Controller A primer on Objective-Oriented Design Building the Button_Fun app EEC492/693/793 - iPhone Application Development
Declared Properties A convenience notation used to replace the declaration and implementation of accessor methods In class interface, declare properties @property BOOL flag Each property specifies a method with the same name as the property Each writable property specifies an additional method of the form setPropertyName In the class implementation, use @synthesize compiler directive to ask the compiler to generate methods @synthesize flag; EEC492/693/793 - iPhone Application Development
Objective-C Properties • The @property declaration (in the .h file) is the Objective-C way of declaring the getters and setters • @property attributes • retain: tells the compiler to send a retain message to any object that we assign to this property => do not garbage collect it • Nonatomic: tells the compiler don’t bother adding thread-safety protections EEC492/693/793 - iPhone Application Development
Objective-C Properties • Dot notation: convenient way to accessing a property • myVar = someObject.foo; • Is equivalent to: myVar = [someObject foo]; • someObject.foo = myVar; • Is equivalent to: [someObject setFoo: myVar]; EEC492/693/793 - iPhone Application Development
Model-View-Controller Model-View-Controller (MVC) is a design pattern commonly used for GUI-based apps Model: the classes that hold your app’s data View: made up of the windows, controls, and other elements that the user can see and interact with Controller: binds the model and view together and is the application logic that decides how to handle the user’s inputs Goal in MVC: make the objects that implement these three types of code as distinct from one another as possible MVC helps ensure maximum reusability EEC492/693/793 - iPhone Application Development
Model Manages the app data and state Not concerned with UI or presentation Often persists somewhere Same model should be reusable, unchanged in different interfaces EEC492/693/793 - iPhone Application Development
View Present the Model to the user in an appropriate interface Allows user to manipulate data Does not store any data Except to cache state Easily reusable & configurable to display different data EEC492/693/793 - iPhone Application Development
Controller Intermediary between Model & View Updates the view when the model changes Updates the model when the user manipulates the view Typically where the app logic lives EEC492/693/793 - iPhone Application Development
Model-View-Controller Controller Model View EEC492/693/793 - iPhone Application Development
Model-View-Controller Controller outlets actions Model Object EEC492/693/793 - iPhone Application Development
Object Oriented Programming Objects Classes Inheritance Polymorphism EEC492/693/793 - iPhone Application Development
Objects Objects are the central idea behind OOP An object is a bundle of (instance) variables and related methods The concept of object makes it easier to capture and simulate the states and activities of real word objects programmatically A method is an operation which can modify an object’s behavior (by changing its variables) In general, variables in an object should be modified by one of the object’s methods EEC492/693/793 - iPhone Application Development
Classes A class is a blueprint (i.e. specification) for an object A class does not represent an object; it represents all the information a typically object should have as well as all the methods it should have Instantiation: an object is created based on the class specification EEC492/693/793 - iPhone Application Development
Inheritance A class C2 can be defined to be the subclass of another class C1 C2 inherits C1, i.e., C2 will have everything C1 has (both data and behavior) C2 can use, augment, or replace C2’s methods Memory management NSObject C1 (superclass) Generic behaviors UIControl C2 (subclass) Specific behaviors UIButton UITextField EEC492/693/793 - iPhone Application Development
Polymorphism Polymorphism refers to the ability of objects belonging to different classes to respond to the same message, each one according to an appropriate type-specific behavior The programmer does not have to know the exact type of the object in advance => the exact behavior is determined at run-time Different objects involved only need to present a compatible interface to clients Usually, objects types are often implemented as subclasses of the same superclass EEC492/693/793 - iPhone Application Development
Inheritance with Polymorphism If a Dog is commanded to speak(), it may emit a bark If a Pig is asked to speak(), it may respond with an oink Both Dog and Pig inherit speak() from Animal, but their subclass methods override the methods of the superclass Inheritance combined with polymorphism allows class B to inherit from class A without having to retain all features of class A; it can do some of the things that class A does differently Calling code can issue the same command (sending the same message) to their superclass and get different results EEC492/693/793 - iPhone Application Development
Benefits of OOP • Encapsulation: states (variables) are changed via appropriate methods rather than changed directly by calling code • This drastically helps with understanding of the code and eliminating potential bugs • Modularity: different programmers or teams can work on different independent objects • Inheritance & polymorphism EEC492/693/793 - iPhone Application Development
The Button Fun App • Creating the project • Choose view-based application, name it “Button Fun” • Creating the view controller • Outlets and actions • Adding actions and outlets to the view controller EEC492/693/793 - iPhone Application Development
Xcode Generated Code #import <UIKit/UIKit.h> @interface Button_FunViewController : UIViewController { } @end • Our view controller • Button_FunViewController.h • Button_FunViewController.m • Content of the header file • Our view controller is a subclass of UIViewController, which is part of the UIKit EEC492/693/793 - iPhone Application Development
Outlets and Actions • In the button fun app, we will have two buttons and one label, which we will create in the IB. The question is: how do we interact with them programmatically? • The answer is: through outlets and actions • Outlet: a special kind of instance variable by which our controller class can refer to objects in the nib file • E.g., controller can read the text of a label or modify it • Action: interface objects in our nib file can be set up to trigger special methods (i.e., actions) in our controller class • E.g., when a user touches up (i.e., pulls a finger off the screen) within a button, a specific action method should be called EEC492/693/793 - iPhone Application Development
Outlets • Outlets are declared using the keyword IBOutlet, as in the example: • @property (nonatomic, retain) IBOutlet UIButton *myButton; • Actions are methods in the controller class • (IBAction)doSomething:(id)sender; • An action method has to return IBAction type (equivalent to “void”) • If you want to know the control that triggered the action, you should provide the (id)sender argument • In our app, we will use the sender arg. Because we want to know which button was tapped EEC492/693/793 - iPhone Application Development
Add Actions and Outlets to View Controller (part1) #import <UIKit/UIKit.h> @interface Button_FunViewController : UIViewController { UILabel *statusText; } @property (nonatomic, retain) IBOutlet UILabel *statusText; - (IBAction)buttonPressed:(id)sender; @end Add declaration to the header file to: EEC492/693/793 - iPhone Application Development
Add Actions and Outlets to View Controller (part 2) @synthesize statusText; // tell the compiler to automatically create the accessor and mutator methods for us - (IBAction)buttonPressed:(id)sender { NSString *title = [sender titleForState:UIControlStateNormal]; NSString *newText = [[NSString alloc] initWithFormat: @"%@ button pressed.", title]; statusText.text = newText; [newText release]; } Add implementation to Button_FunViewController.m EEC492/693/793 - iPhone Application Development
Add Actions and Outlets to View Controller • (void)viewDidUnload { • self.statusText = nil; • //you need to set any outlets your class has to nil • // in viewDidUnload. • } • - (void)dealloc { • [statusText release]; //release the outlet • [super dealloc]; • } • @end Add implementation to Button_FunViewController.m EEC492/693/793 - iPhone Application Development
Creating the View in IB • Double-click Button_FunViewController.xib • Add a label towards the bottom of the view, and two buttons side-by-side in the middle of the view • Pay attention to the guidelines while placing the controls • Change the text alignment to centered at the inspector • Double-click the label and remove existing text • Add text “Left” to the left button, “Right” to the right button EEC492/693/793 - iPhone Application Development
Connecting Everything Connecting outlets: control drag from File’s Owner to the control EEC492/693/793 - iPhone Application Development
Specifying Actions • Bring up connections inspector by pressing ⌘2 (or Tools -> Connection Inspector) • For left button, under the heading Events, select “Touch Up Inside” • This is an event when the user’s finger lifts up from the screen and the last place it touched before lifting was inside the button • Associate the event with the action • Click the circle right of “Touch Up Inside” and drag over to the “File’s Owner” icon • When the gray menu pops up, select “buttonPressed:” • Repeat for right button EEC492/693/793 - iPhone Application Development
Trying It Out • Save your project • Select “Build and Run” • Play with the app in the simulator • Test on your device • Rebuild the app for your device • ssh the app folder to your device • Respring on your device, you will see your app • Tap on your app to start it EEC492/693/793 - iPhone Application Development