Presentation is loading. Please wait.

Presentation is loading. Please wait.

Passing data between storyboard views Singleton pattern.

Similar presentations


Presentation on theme: "Passing data between storyboard views Singleton pattern."— Presentation transcript:

1 Passing data between storyboard views Singleton pattern

2 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.

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

4 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

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

6 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) theDist; @end

7 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; }

8 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; }

9 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; NSLog(@"Time to planet %@ from %@ is %.2f", thePlanet, planet, theTime); }

10 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 stringWithFormat:@"I am an alien that lives on %@ with %d eyes!", planet, numEyes ]; return aboutMe; } // description @end

11 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

12 CMPSharedAlien.h #import #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.

13 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 = [[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.

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

15 CMPViewController.h This class will control both the views. #import #import "alien.h" @interface CMPViewController : UIViewController @property (weak, nonatomic) alien *myAlien; @property (weak, nonatomic) IBOutlet UILabel *myLabel; @end Contains an instance of the alien class You must connect this to the label on each view in the storyboard

16 CMPViewController.m #import "CMPViewController.h" #import "CMPSharedAlien.h” @implementation CMPViewController @synthesize myAlien;

17 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

18 CMPViewController.m − (void)viewWillAppear:(BOOL)animated{ NSString *msg = [[NSString alloc] 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.

19 Segues Can get a pointer to the destination class in a segue − (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] isEqualToString:@"SendInfo"]) { Destination *detailViewController = [segue destinationViewController]; NSLog(@"text : %@",self.CaptureInformation.text); //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.


Download ppt "Passing data between storyboard views Singleton pattern."

Similar presentations


Ads by Google