僅僅因爲數據是在「同一個文件」並不意味着任何東西在運行時應用:當前一個共享模型對象積累。您仍然在創建RegistrationViewController
類的多個單獨實例。因此,您需要在這些單獨的實例之間傳遞數據,就像完全不同的類一樣。
由於您使用的是故事板和segues,因此方法-prepareForSegue:sender:
是一個很好的考慮這樣做的地方。
爲了記錄在案,我同意@ dasblinkenlight的建議,存儲在模型對象,而不是單個字段這個數據,但使用「共享實例」如果你沒有不同意。通過模型對象的一個例子是這樣的:
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;
}
感謝您的建議。您正確使用共享實例是一個正確的選擇。你能給我一個簡單的想法,告訴我如何創建一個共享實例,以便它可以適用於所有這三個viewcontollers。我創建了一個新的「AllUsers.h」(NSObject)類文件。 – Jimmy
我使用的是Objective-C語言。 – Jimmy
@Jimmy對不起,我錯過了Objective-C部分。 [這是一個在Objective-C中使用單例模型的例子Q&A](http://stackoverflow.com/a/11945106/335858)。這是相當複雜的,但它本質上是一個複製粘貼練習。 – dasblinkenlight