0

I am having problem here regarding data lost on NSDictionary (or whatever data type I am trying to pass between view controllers). I am passing the data as I will show on code below. But after passing and then accessing the data again, I received a null object.

FirstViewController.m

-(void)showSecondViewController{
  SecondViewController *secondVC = [SecondViewController new];
  NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
  [passedData setObject:@"First Object" forKey:@"first_obj"];
  [passedData setObject:@"Second Object" forKey:@"second_obj"];
  [secondVC setData:passedData];
  [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
}

In my SecondViewController.h

 @interface SecondViewController : UIViewController
{
   NSDictionary *passedDataContainer;
}

-(void)setData(NSDictionary *)data;

And on SecondViewController.m

-(void)setData(NSDictionary *)data{
   passedDataContainer = data;
}

From this I am assuming that I have my NSDictionary from my FirstViewController, but when I used it, say for example...

-(void)usePassedData{
  NSLog(@"Display Passed Data from FirstViewController: %@",passedDataContainer);
}

In this code snippet, the output will be

Display Passed Data from FirstViewController: (null)

I hope I have explained the case well. I have checked some possible fixes that I found here on SO like for example, this post ---> iOS NSDictionary pass through function. I will be glad if I will know what is the cause of this, I am new to Objective-C.

P.S. I am using ARC.

Thanks.

Community
  • 1
  • 1
caribbean
  • 748
  • 1
  • 9
  • 29

4 Answers4

0

If you want to pass the data from secondviewcontroller to first view controller then you can used CUSTOM DELEGATE , using custome delegate you can pass the data from secondviewcintroller to firstviewcontroller.

This link may be helpful to you. This is second link to understand the delegate.

Community
  • 1
  • 1
Maulik Kundaliya
  • 452
  • 3
  • 17
0

There are many ways to achieve for your solution.

  1. NSUserDefaults.
  2. Delegates(Custom).

For Your problem :. Use the @PRoperty (Strong) reference to the NSDictionary and refer it from FirstViewController with the proper alloc of instance of SecondViewController.

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
0

try like this, hope this helps u :)

in your SecondViewController.h

 @interface SecondViewController : UIViewController
 {
   NSDictionary *passedDataContainer;
 }

@property (nonatomic, retain) NSDictionary *data;


and in SecondViewController.m


   @synthesize data; //just synthesize it


and your FirstViewController.m


-(void)showSecondViewController{
 SecondViewController *secondVC = [SecondViewController new];
 NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
 [passedData setObject:@"First Object" forKey:@"first_obj"];
 [passedData setObject:@"Second Object" forKey:@"second_obj"];
  secondVC.data = passedData; //[secondVC setData:passedData];//change this
 [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
}


Shankar BS
  • 8,394
  • 6
  • 41
  • 53
0

Form your explanation, you are using storyboard segue to transition from FirstViewController to SecondViewController. You need to pass data in prepareForSegue method instead of showSecondViewController method.

try this

FirstViewController.m

-(void)showSecondViewController{

  [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
     if ([segue.identifier isEqualToString:@"SecondViewControllerSegue"]){
     NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
     [passedData setObject:@"First Object" forKey:@"first_obj"];
     [passedData setObject:@"Second Object" forKey:@"second_obj"];

     SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
    [secondVC setData:passedData];
}

In SecondViewController.h

@property(strong, nonatomic) NSDictionary *data;

In SecondViewController.m

-(void)usePassedData{
  NSLog(@"Display Passed Data from FirstViewController: %@",data);
}
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60