我目前的设计中有四个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]; } }
@R_403_323@
即使这里的大多数用户告诉你,也请不要使用单身人士.由于几个原因,它会违反
SOLID-Principles.
而只是将对象从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