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.