Presentation is loading. Please wait.

Presentation is loading. Please wait.

EEC-492/693/793 iPhone Application Development

Similar presentations


Presentation on theme: "EEC-492/693/793 iPhone Application Development"— Presentation transcript:

1 EEC-492/693/793 iPhone Application Development
Lecture 3 Wenbing Zhao & Nigamanth Sridhar 11/17/2018 EEC492/693/793 - iPhone Application Development

2 EEC492/693/793 - iPhone Application Development
Outline Objective-C Property Model-View-Controller A primer on Objective-Oriented Design Building the Button_Fun app 11/17/2018 EEC492/693/793 - iPhone Application Development

3 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, compiler directive to ask the compiler to generate methods @synthesize flag; 11/17/2018 EEC492/693/793 - iPhone Application Development

4 Objective-C Properties
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 11/17/2018 EEC492/693/793 - iPhone Application Development

5 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]; 11/17/2018 EEC492/693/793 - iPhone Application Development

6 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 11/17/2018 EEC492/693/793 - iPhone Application Development

7 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 11/17/2018 EEC492/693/793 - iPhone Application Development

8 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 11/17/2018 EEC492/693/793 - iPhone Application Development

9 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 11/17/2018 EEC492/693/793 - iPhone Application Development

10 Model-View-Controller
11/17/2018 EEC492/693/793 - iPhone Application Development

11 Model-View-Controller
outlets actions Model Object 11/17/2018 EEC492/693/793 - iPhone Application Development

12 Object Oriented Programming
Objects Classes Inheritance Polymorphism Taken from 11/17/2018 EEC492/693/793 - iPhone Application Development

13 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 11/17/2018 EEC492/693/793 - iPhone Application Development

14 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 11/17/2018 EEC492/693/793 - iPhone Application Development

15 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 To do: what UIControl generic behavior is defined? Memory management NSObject C1 (superclass) Generic behaviors UIControl C2 (subclass) Specific behaviors UIButton UITextField 11/17/2018 EEC492/693/793 - iPhone Application Development

16 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 11/17/2018 EEC492/693/793 - iPhone Application Development

17 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 11/17/2018 EEC492/693/793 - iPhone Application Development

18 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 11/17/2018 EEC492/693/793 - iPhone Application Development

19 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 Chapter 3 11/17/2018 EEC492/693/793 - iPhone Application Development

20 EEC492/693/793 - iPhone Application Development
Xcode Generated Code 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 #import <UIKit/UIKit.h> @interface Button_FunViewController : UIViewController { } @end 11/17/2018 EEC492/693/793 - iPhone Application Development

21 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 11/17/2018 EEC492/693/793 - iPhone Application Development

22 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 11/17/2018 EEC492/693/793 - iPhone Application Development

23 Add Actions and Outlets to View Controller (part1)
Add declaration to the header file to: #import <UIKit/UIKit.h> @interface Button_FunViewController : UIViewController { UILabel *statusText; } @property (nonatomic, retain) IBOutlet UILabel *statusText; - (IBAction)buttonPressed:(id)sender; @end 11/17/2018 EEC492/693/793 - iPhone Application Development

24 Add Actions and Outlets to View Controller (part 2)
Add implementation to Button_FunViewController.m @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]; } Sender will point to the button that was tapped 11/17/2018 EEC492/693/793 - iPhone Application Development

25 Add Actions and Outlets to View Controller
Add implementation to Button_FunViewController.m (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 11/17/2018 EEC492/693/793 - iPhone Application Development

26 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 11/17/2018 EEC492/693/793 - iPhone Application Development

27 Connecting Everything
Connecting outlets: control drag from File’s Owner to the control 11/17/2018 EEC492/693/793 - iPhone Application Development

28 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 11/17/2018 EEC492/693/793 - iPhone Application Development

29 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 11/17/2018 EEC492/693/793 - iPhone Application Development


Download ppt "EEC-492/693/793 iPhone Application Development"

Similar presentations


Ads by Google