1

I have 2 viewcontrollers which are named viewController and secondViewController. I want to get data and send it to secondViewController using delegates. Also i have a array in secondViewController, when each data came from VC1 it must store data like;

segue1,first data came -> arrayElements {firstData} segue2,second data came -> arrayElements {firstData, secondData}

But everytime the secondViewController comes into screen, it losts previous data(data from previous segues). Here my code;

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 (i copied only required part)

- (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];

}

Here is the visual; http://i.hizliresim.com/ql8aJ3.png

As i said every time i click the show button to segue, i lost all my previous datas in the ViewController2.

I read previous questions like that, but most of them are about just passing one data. I'm very confused.

How can I stored these data using delegates without loss previous ones.

3
  • Because every time you execute the segue it is a brand new object instance of the second view controller. Do you know the difference between a class and an instance of a class? Commented Aug 5, 2015 at 21:54
  • Thx for reply, yeah i know but how can i pass the data without creating a new instance then ? Commented Aug 5, 2015 at 21:57
  • Your second view controller could save its data somewhere (such as NSUserDefaults) then each time it gets created it reads back the data it has previously stored before adding the new data passed to it from the first view controller. But that is messy and its probably now is time for you to assess if your design should have a Model, and it would be the model that is storing and mediating the data. Keeping the second view controller permanently loaded is a bit hacky and inefficient. Commented Aug 5, 2015 at 22:25

2 Answers 2

1

The problem with your code is that you are initializing a new SecondViewController in the sendButton action every time.

So every time sendButton is tapped, svc.receivedData is an empty (new) array

Consider keep svc as a local variable and call init only one time.

Something like:

In FirstViewController.h, add this line:

@property (strong, nonatomic) SecondViewController *svc;

And these lines to FirstViewController.m

- (IBAction)sendButton:(UIButton *)sender {
    ...
    if(self.svc == nil){ 
        self.svc = [[SecondViewController alloc] init];
        self.delegate = self.svc;
    }
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

I understand, but you said that add this line to firstViewController.h: " @property (strong, nonatomic) NSString *myData; " which is already here. You mean "@property (strong, nonatomic) SecondViewController *svc;" ? So I can use svc as a self.svc
0

You Have a navigation controller so when you segue from firstViewController to show secondViewcontroller, it push the secondViewcontroller to navigation stack. When tap back button to go back to firstViewController, it pop the secondViewController from navigation Stack and will be deallocated so no data or views will be there after that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.