170 likes | 288 Views
Passing data between storyboard views. Singleton pattern. Beginning. Give the views a label. Create a project with storyboards Create two views Embed in tabViewController See last lecture. Beginning. Create a view controller for the two views. View controller for the two views.
E N D
Passing data between storyboard views Singleton pattern
Beginning Give the views a label • Create a project with storyboards • Create two views • Embed in tabViewController • See last lecture
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 #import <Foundation/Foundation.h> @interface alien : NSObject { intnumEyes; NSString *planet; double distanceToHome; double speedSpaceShip; } @property (nonatomic, assign) intnumEyes; @property (nonatomic, retain) NSString *planet; @property (nonatomic, assign) double distanceToHome; @property (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) theSpeedatDistance: (double) theDist; −(void) goToPlanet: (NSString *) thePlanetwithDistance: (double) theDist; @end
The model: alien.m #import "alien.h" @implementation alien @synthesize numEyes, planet, distanceToHome; - (id) init { if (self = [super init]) { numEyes = 4; planet = @"Neptune"; distanceToHome = 1000000; 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) theSpeedatDistance: (double) theDist; { double theTime = theDist / theSpeed; return theTime; } − (void) goToPlanet: (NSString *) thePlanetwithDistance: (double) theDist { double theTime; theTime = theDist / speedSpaceShip; NSLog(@"Time 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 = [NSStringstringWithFormat:@"I am an alien that lives on %@ with %d eyes!", planet, numEyes ]; return aboutMe; } // description @end
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 <Foundation/Foundation.h> #import "alien.h" @interface CMPSharedAlien : NSObject{ alien *theAlien; } +(CMPSharedAlien *)sharedAlien; − (void)setTheAlien:(alien *)newAlien; − (alien *)getTheAlien; @end 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 *sharedAlien; @implementation CMPSharedAlien −(id)init{ self = [super init]; theAlien = [alien new]; return self; } +(CMPSharedAlien *)sharedAlien{ if (!sharedAlien) { sharedAlien = [[CMPSharedAlienalloc] init]; } return sharedAlien; } A static variable. It will be created only once and will be shared by all instances of the CMPSharedAlien class. When initialized a new alien instance is created. 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.
CMPSharedAlien.m Instance methods of the CMPSharedAlien class − (void)setTheAlien:(alien *)newAlien{ theAlien = newAlien; } − (alien *)getTheAlien{ return theAlien; } @end
CMPViewController.h • This class will control both the views. #import <UIKit/UIKit.h> #import "alien.h" @interface CMPViewController : UIViewController @property (weak, nonatomic) alien *myAlien; @property (weak, nonatomic) IBOutletUILabel *myLabel; @end 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 "CMPSharedAlien.h” @implementation CMPViewController @synthesize myAlien;
CMPViewController.m − (void)viewDidLoad { [super viewDidLoad]; if (self.myAlien == nil){ CMPSharedAlien * mySharedAlien = [CMPSharedAliensharedAlien]; myAlien = mySharedAlien.getTheAlien; } self.myLabel.text = self.myAlien.planet; } If myAlienis 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 = [[NSStringalloc] initWithFormat:@"%@ 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.