Passing data between storyboard views Singleton pattern.

Slides:



Advertisements
Similar presentations
Objective-C Lecture 2 Memory management Memory management in OC is semi- automatic: The programmer must allocate memory for objects either a) explicitly.
Advertisements

Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns.
Table Views UITableView. Overview Table view basics Tables display lists of data Each item in a tables list is a row Tables can have an unlimited number.
suppose you had Bumblebee, Jet, and Bird classes. All three, although unrelated, implement the flying behavior, and so you could say they are each a Flyer.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Written by: Dr. JJ Shepherd
TableView and TableViewController
IOS and AddressBook CS4521. Address Book UI Framework Exploring Contacts.
Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
View Controllers (second part) Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson.
IPhone Development Crash Course By Dylan Harris
Intro to Objective-C Syntax: What’s most confusing about Objective-C? Most class names start with NS: NSString, NSObject Parameter lists are not comma.
3-Jul-15 Chapter 2 iPhone App Store and App Business Issues.
NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman.
Storyboards Managing multiple views. Overview Create a single view application Give the project a name and click “Use Storyboards” and “Use Automatic.
Review of C++ Programming Part II Sheng-Fang Huang.
Sprite Animation CSE 391 Fall 2012 Tony Scarlatos.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Refactoring Moving a console app to a UI app. Refactoring Goal: change a console app to a UI app Principles: The main.m goes away The local variables.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Xcode testing Using XCTest.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Objective-C and Xcode (Part 3) FA 175 Intro to Mobile App Development.
Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.
Tabbed Views UITabBarController. Controller Architecture UITabBarController Controls the first view that the user sees The view controller class (and.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Navigation in iPads splitViewController. Overview Create a Master-Detail application Switch Device Family to iPad Give the project a name and click “Use.
Managing Multiple Views and Segues FA 172 Intro to Mobile App Development.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Create a Web View App Step-by-Step. Step 1 Create a new project in XCode using the "Single View Application" option.
Objective C. Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция.
View Controllers Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Nav Controllers UINavigationController. Overview Nav Controller basics Like a tabview controller, a navViewController manages views A navigationViewController.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
OOP with Objective-C Categories, Protocols and Declared Properties.
The Controller in MVC of iOS CS4521. The controller in the MVC  Controller  Knows about model and view objects  The brains of the operation  Manages.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
The iOS Platform and SDK. iOS iPhoneiPad Mini iPad.
1 Reverse a String iPhone/iPad, iOS Development Tutorial.
OBJECTIVE C Kurt Ladendorf. General Knowledge  Super set of C  Smalltalk  Mainly used for iOS and other Apple development.
Classes, Interfaces and Packages
Const declares immutable value void Test(char* t) { t++;// OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2(const char* t) {
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Introduction to Objective-C and Xcode (Part 4) FA 175 Intro to Mobile App Development.
Class Fundamentals BCIS 3680 Enterprise Programming.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java: Base Types All information has a type or class designation
Pointers and Dynamic Arrays
Objects as a programming concept
Java: Base Types All information has a type or class designation
iOS - First Application Anatomy
Class Definitions and Writing Methods
CSC 253 Lecture 8.
PH page GoF Singleton p Emanuel Ekstrom.
EEC-492/693/793 iPhone Application Development
EEC-492/693/793 iPhone Application Development
CSC 253 Lecture 8.
Interface.
CS212: Object Oriented Analysis and Design
EEC-492/693/793 iPhone Application Development
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
EEC-492/693/793 iPhone Application Development
Presentation transcript:

Passing data between storyboard views Singleton pattern

Methods Two ways to pass data Singleton. Create a global class that can be accessed via class (not instance) methods Segues. If you’re performing a segue, you can get the destination class and modify a property.

Beginning Create a project with storyboards Create two views Embed in tabViewController See last lecture Give the views a label

Beginning Create a view controller for the two views. View controller for the two views Make sure that the 2 views in the MainStoryboard are of type CMPViewController

The model: alien.h We’ll use the alien class created at the beginning of the semester. Import or create alien : NSObject { int numEyes; NSString *planet; double distanceToHome; double speedSpaceShip; (nonatomic, assign) int (nonatomic, retain) NSString (nonatomic, assign) double (nonatomic, assign) double speedSpaceShip; The model

The model: alien.h /********************** methods ****************************************/ // init does not have to be in the interface; all other constructors do − (id) init; − (id) initWithNum: (int) a andDistance: (double) b andPlanet: (NSString *) c; // method preceded by a "-" is an instance method // method preceded by a "+" is a class method − (double) calcTimeToHome; − (double) timeToPlace: (double) dist; - − (double) timeWithSpeed: (double) theSpeed atDistance: (double) theDist; − (void) goToPlanet: (NSString *) thePlanet withDistance: (double)

The model: alien.m #import numEyes, planet, distanceToHome; - (id) init { if (self = [super init]) { numEyes = 4; planet distanceToHome = ; speedSpaceShip = 1000; return (self); } return nil; }

The model: alien.m − (id) initWithNum: (int) a andDistance: (double) b andPlanet: (NSString *) c { if (self = [super init]) { numEyes = 4; planet = c; distanceToHome = b; speedSpaceShip = a; return (self); } return nil; } // method preceded by a "-" is an instance method // method preceded by a "+" is a class method − (double) calcTimeToHome { double theTime; theTime = distanceToHome / speedSpaceShip; return theTime; }

The model: alien.m − (double) timeToPlace: (double) dist; { double theTime = dist / speedSpaceShip; return theTime; } − (double) timeWithSpeed: (double) theSpeed atDistance: (double) theDist; { double theTime = theDist / theSpeed; return theTime; } − (void) goToPlanet: (NSString *) thePlanet withDistance: (double) theDist { double theTime; theTime = theDist / speedSpaceShip; to planet from is %.2f", thePlanet, planet, theTime); }

The model: alien.m // This method is not in the.h file. It’s called when you print an instance of the class // The method is used to allow a class to print out a string describing itself. − (NSString *) description { NSString *aboutMe; aboutMe = [NSString am an alien that lives on with %d eyes!", planet, numEyes ]; return aboutMe; } //

Singletons Goal: share an instance of an alien among the two views. Concept: Create a new class Will contain a static instance of the model class (alien) Will contain a class method A class method can be used without creating an instance The class method will return an instance of the class itself The instance can return the static instance of the alien class

CMPSharedAlien.h #import #import CMPSharedAlien : NSObject{ alien *theAlien; } +(CMPSharedAlien *)sharedAlien; − (void)setTheAlien:(alien *)newAlien; − (alien The instance of the alien class that will be shared The class method; returns an instance of this class. Note the + sign in front of the method.

CMPSharedAlien.m #import "CMPSharedAlien.h" static CMPSharedAlien CMPSharedAlien − (id)init{ self = [super init]; theAlien = [alien new]; return self; } +(CMPSharedAlien *)sharedAlien{ if (!sharedAlien) { sharedAlien = [[CMPSharedAlien alloc] init]; } return sharedAlien; } A static variable. It will be created only once and will be shared by all instances of the CMPSharedAlien class. The class method; If the static variable does not exist, it allocates and inits it. This is only done once since the variable is shared among all instances. Returns the static instance of this class. When initialized a new alien instance is created.

CMPSharedAlien.m Instance methods of the CMPSharedAlien class − (void)setTheAlien:(alien *)newAlien{ theAlien = newAlien; } − (alien *)getTheAlien{ return theAlien;

CMPViewController.h This class will control both the views. #import #import CMPViewController : (weak, nonatomic) alien (weak, nonatomic) IBOutlet UILabel Contains an instance of the alien class You must connect this to the label on each view in the storyboard

CMPViewController.m #import "CMPViewController.h" #import myAlien;

CMPViewController.m − (void)viewDidLoad { [super viewDidLoad]; if (self.myAlien == nil){ CMPSharedAlien * mySharedAlien = [CMPSharedAlien sharedAlien]; myAlien = mySharedAlien.getTheAlien; } self.myLabel.text = self.myAlien.planet; } If myAlien is nil then we haven’t gotten the alien instance from the singleton class Call the class method; it returns the shared instance of the alien class Get the shared instance of the alien class Set the text label

CMPViewController.m − (void)viewWillAppear:(BOOL)animated{ NSString *msg = [[NSString alloc] is %.2f from home", myAlien.planet, myAlien.distanceToHome]; myAlien.distanceToHome += 10000; self.myLabel.text = msg; } When the view appears it displays the shared alien’s planet and then updates its distanceToHome value each view has it’s own instance of this controller, but each of the instances will contain the same instance of the alien class. So when one view appears it updates the shared alien instance. When the next view appears, it shows the updated value.

Segues Can get a pointer to the destination class in a segue − (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] { Destination *detailViewController = [segue destinationViewController]; : //This is the id infoRequest, which is a pointer to the object //Look at the viewDidLoad in the Destination implementation. detailViewController.infoRequest = self.CaptureInformation.text; } Must give the segue a unique name in the storyboard This is the class of the destination of the segue This is the property in the destination class that we’ll use to pass information.