将数据从FirstViewController传递到LastViewController

我目前的设计中有四个viewControllers,我正在设计一个销售产品的应用程序。

FirstViewController获取产品图像,当用户点击下一个按钮时,它将用户带到用户描述产品的secondviewcontroller ,然后用户点击下一个按钮,该按钮将用户带到输入价格和条件的thirdViewcontroller 。 在lastviewcontolller有一个post按钮,用于将产品信息发送到服务器。 我正在使用POST方法。

以下segue方法不适合我想要的,因为它将firstviewcontroller对象(产品图像)发送到secondviewcontoller ,然后secondviewcontroller也应该将产品图像转发到thirdviewcontoller ,依此类推。 我不认为这是一种可行的方法。

我想知道从第一页到最后一页收集信息的最佳方式是什么,然后发送。 处理该问题的最佳方法是什么? 我在viewcontrollers之间使用segue。

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Make sure your segue name in storyboard is the same as this line if ([[segue identifier] isEqualToString:@"isSecond"]) { // Get reference to the destination view controller SecondViewController *vc = [segue destinationViewController]; // Pass any objects to the view controller here, like... [vc setMyProductImage:productImage]; } } 

即使这里的大多数用户告诉你,也请不要使用单身人士。 由于几个原因,它会违反SOLID原则 。

而只是将对象从ViewController传递给ViewController。

如果所有ViewController都期望相同的模型类,则可以创建具有模型属性的公共基类。

它可以有这种方法

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.destinationViewControler isKindOfClass:[ProductAwareBaseViewController class]]) { ProductAwareBaseViewController *vc = (ProductAwareBaseViewController *)segue.destinationViewControler; vc.product = self.product; } } 

我创建了一个示例项目: https : //github.com/vikingosegundo/ProductWizard

注意,所有视图控制器都派生自ProductAwareBaseViewController

 @import UIKit; @class Product; @interface ProductAwareBaseViewController : UIViewController @property (nonatomic, strong) Product *product; @end 

 #import "ProductAwareBaseViewController.h" #import "Product.h" @interface ProductAwareBaseViewController () @end @implementation ProductAwareBaseViewController - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.destinationViewController isKindOfClass:[ProductAwareBaseViewController class]]) { ProductAwareBaseViewController *vc = (ProductAwareBaseViewController *)segue.destinationViewController; vc.product = self.product; } } @end 

此ViewController知道如何将类Product的模型数据传递给ProductAwareBaseViewController其他实例及其子类。

所有其他视图控制器不处理传递数据,只是将每个数据部分(名称,描述,价格)添加到模型并显示它。

即:

 #import "EditNameProductViewController.h" #import "Product.h" @interface EditNameProductViewController () @property (weak, nonatomic) IBOutlet UITextField *nameField; @end @implementation EditNameProductViewController - (void)viewDidLoad { [super viewDidLoad]; self.product = [[Product alloc] init]; } - (IBAction)continueTapped:(id)sender { self.product.productName = self.nameField.text; } @end 

 #import "EditDescriptionProductViewController.h" #import "Product.h" @interface EditDescriptionProductViewController () @property (weak, nonatomic) IBOutlet UITextField *descriptionField; @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @end @implementation EditDescriptionProductViewController - (void)viewDidLoad { [super viewDidLoad]; self.nameLabel.text = self.product.productName; } - (IBAction)continueTapped:(id)sender { self.product.productDescription = self.descriptionField.text; } @end 

创建一个对象作为应用程序的数据模型。 它可以是单例,也可以是可从已知位置获得的普通对象…例如由app委托拥有。

有新信息时更新模型,并在需要显示时从模型中读取。 使用prepareForSegue:和链接控制器对于简单的事情可能是可以接受的,但它实际上不能很好地扩展。

执行此操作的一种方法是在第一个视图控制器中创建可变字典(或带变量的自定义对象)。 然后,您将从第一个视图控制器传递弱引用到可变字典/对象的第二/第三/第四视图控制器。 每个视图控制器都能够将数据设置为字典/对象,最后一个将能够处理信息。

另一种方法是使用要存储的变量创建一个简单的单例类。 第一个视图控制器将重置单例变量。 然后让每个视图控制器访问单例并在那里存储它们的值,最后一个视图控制器将处理来自单例的值。

这取决于您收集的数据量以及您个人喜欢的数据。