简体   繁体   English

在同一视图控制器类之间传递数据

[英]Pass data between same view controller class

I've start learning iOS development (with Objective-C) and I'm having the following problem. 我已经开始学习iOS开发(使用Objective-C),并且遇到了以下问题。

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 . 视图控制器1具有用于fnamelname字段。
  • View controller 2 has a field for email . View Controller 2有一个用于email的字段。
  • View controller 3 has a field for password and a "save" button. View Controller 3具有一个password字段和一个“保存”按钮。

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. 现在,我创建了一个名为RegistrationViewController的新UIViewController类文件,并将该类分配给上述所有视图控制器,以便可以将所有与注册相关的代码写入一个文件中。

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 . 当我单击最后一个视图控制器上的“保存”按钮时,无法获得fnamelnameemail

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. 我已经使用了推推功能从一个viewcontoller转到下一个viewcontroller。

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 . Push segue推送视图控制器的全新实例,这意味着当前位于顶部的视图控制器本身将只具有设置的值,而其所有先前版本中设置的值将为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: 相反,您应该按照Model-View-Controller设计模式的建议,在模型类的共享实例上设置它们:

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. 您仍在创建RegistrationViewController类的多个单独实例。 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. 由于您使用的是故事板和脚本,因此-prepareForSegue:sender:方法是进行此操作的好地方。


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. 作为记录,我同意@dasblinkenlight的建议,将数据存储在模型对象而不是单个字段中,但是如果不需要,则不同意使用“共享实例”。 An example of passing a model object would look like this: 传递模型对象的示例如下所示:

User.h User.h

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

RegistrationViewController.h RegistrationViewController.h

@property (nonatomic, strong) User *user;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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