【问题标题】:Passing Data to Array Between Different ViewControllers在不同的 ViewController 之间将数据传递给数组
【发布时间】:2015-08-05 21:24:06
【问题描述】:

我有 2 个视图控制器,分别名为 viewController 和 secondViewController。我想获取数据并使用委托将其发送到 secondViewController。另外我在 secondViewController 中有一个数组,当每个数据来自 VC1 时,它必须存储类似的数据;

segue1,first data come -> arrayElements {firstData} segue2,第二个数据来了 -> arrayElements {firstData, secondData}

但是每次 secondViewController 进入屏幕时,它都会丢失以前的数据(来自以前的 segues 的数据)。这是我的代码;

FirstViewController.h

@protocol CustomDelegate <NSObject>

-(void)passData: (NSString*)data_in;

@end

@interface FirstViewController : UIViewController

@property (strong, nonatomic) NSString *myData;
@property (nonatomic, weak)id<CustomDelegate>delegate;

@end

FirstViewController.m(我只复制了需要的部分)

- (IBAction)sendButton:(UIButton *)sender {

    SecondViewController *svc = [[SecondViewController alloc] init];
    self.delegate = svc;

    [self.delegate passData:self.myData];
}

SecondViewController.h

import things here..
@interface SecondViewController : UIViewController <CustomDelegate>

@property (strong, nonatomic) NSString *receivedData;
@property (strong, nonatomic) NSMutableArray* receivedDataArray;

@end

SecondViewController.m

//declerations, properties, lazy instantiation for array here

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];

    self.receviedDataLabel.text = self.receivedData;


}


-(void)passData:(NSString *)data_in{

    self.receivedData = data_in;
    [self.receivedDataArray addObject:data_in];

}

这是视觉效果; http://i.hizliresim.com/ql8aJ3.png

正如我所说的,每次单击显示按钮进行转场时,我都丢失了 ViewController2 中所有以前的数据。

我读过以前的类似问题,但其中大多数只是传递一个数据。我很困惑。

如何使用委托存储这些数据而不会丢失以前的数据。

【问题讨论】:

  • 因为每次执行 segue 时,它​​都是第二个视图控制器的全新对象实例。你知道类和类的实例的区别吗?
  • 谢谢回复,是的,我知道,但是我怎样才能在不创建新实例的情况下传递数据呢?
  • 您的第二个视图控制器可以将其数据保存在某处(例如 NSUserDefaults),然后每次创建它时,它都会在添加从第一个视图控制器传递给它的新数据之前读回之前存储的数据.但这很麻烦,现在可能是您评估您的设计是否应该有一个模型的时候了,它应该是存储和调解数据的模型。永久加载第二个视图控制器有点笨拙且效率低下。

标签: ios delegates segue viewcontroller


【解决方案1】:

您的代码的问题是您每次都在 sendButton 操作中初始化一个新的 SecondViewController

所以每次sendButton 被点击时,svc.receivedData 都是一个空(新)数组

考虑将svc 保留为局部变量并仅调用一次init

类似:

FirstViewController.h中,添加这一行:

@property (strong, nonatomic) SecondViewController *svc;

这些行发给FirstViewController.m

- (IBAction)sendButton:(UIButton *)sender {
    ...
    if(self.svc == nil){ 
        self.svc = [[SecondViewController alloc] init];
        self.delegate = self.svc;
    }
    ...
}

【讨论】:

  • 我明白了,但是你说在 firstViewController.h 中添加这一行:“@property (strong, nonatomic) NSString *myData;”已经在这里了。你的意思是“@property (strong, nonatomic) SecondViewController *svc;” ?所以我可以使用 svc 作为 self.svc
【解决方案2】:

你有一个导航控制器,所以当你从 firstViewController 切换到显示 secondViewcontroller 时,它会将 secondViewcontroller 推送到导航堆栈。 当点击返回按钮返回 firstViewController 时,它会从导航堆栈中弹出 secondViewController 并将被释放,因此之后将没有数据或视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2017-01-22
    • 2019-12-05
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多