简体   繁体   中英

Pass data between same view controller class

I've start learning iOS development (with Objective-C) and I'm having the following problem.

What I did so far:

I want to divide the registration process into a number of view controllers. For example:

  • View controller 1 has fields for fname and lname .
  • View controller 2 has a field for email .
  • View controller 3 has a field for password and a "save" button.

Now, I created a new UIViewController class file named RegistrationViewController and assigned that class to all view controllers above, so that I can write all registration-related code in a one file.

I've used a push segue to get to the next view controller from the current view controller.

ISSUE

When I click the "save" button on the last view controller, I'm not able to get the values for fname , lname , and email .

Why is that so? I mean, I'm in the same file. Shouldn't the value be stored for those properties?

I've used a push segue to get to the next viewcontroller form a viewcontoller.

Push segue pushes a brand-new instance of the view controller, meaning that the view controller currently on the top would have only the values set in itself, while values set in all of its predecessors would be nil .

You should not be relying on storing values in fields of your view controller. Instead, you should set them on the shared instance of your model class, as suggested by the Model-View-Controller design pattern:

class Model {
    static let sharedInstance = Model()
    var fName : String
    var lName : String
    ...
}

When you detect that the next view controller is about to open, store the state accumulated by the current one in the shared model object:

Model.sharedInstance.fName = self.fName
Model.sharedInstance.fName = self.lName
...

Just because data is in the "same file" doesn't mean anything to the app at runtime. You're still creating multiple separate instances of your RegistrationViewController class. Therefore, you'll need to pass data between these separate instances, the same way you would for completely different classes.

Since you're using storyboards and segues, the method -prepareForSegue:sender: is a good place to look into doing that.


For the record, I agree with @dasblinkenlight's suggestion to store this data in a model object instead of individual fields, but disagree with using a "shared instance" if you don't have to. An example of passing a model object would look like this:

User.h

@property (nonatomic, strong) NSString *fname;
@property (nonatomic, strong) NSString *lname;
@property (nonatomic, strong) NSString *email;

RegistrationViewController.h

@property (nonatomic, strong) User *user;

RegistrationViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    RegistrationViewController *nextViewController = (RegistrationViewController *)segue.destinationViewController;
    nextViewController.user = self.user;
}

- (IBAction)doSaveAction:(id)sender {
    NSLog("email: %@", self.user.email;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM