1 / 17

Passing data between storyboard 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.

garin
Télécharger la présentation

Passing data between storyboard views

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Passing data between storyboard views Singleton pattern

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related