jrock007
jrock007

Reputation: 105

Passing data between view controllers ios 7

I'm working on an app and I need to pass data between view controllers. I know this is a common question but I couldn't find an answer for my problem : I'm able to pass data from the FirstViewController (MasterViewController in my case) to the SecondViewController (SettingsViewController) but not the reverse. What happens is that I call a method from the FirstViewController in my SecondViewController.m file. This works and it logs the data. But when I quit the SecondViewController (using [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];) the data is reset. I tried using other methods to pass data but it didn't work. I'm using this code to pass data:

MasterViewController *vc = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];

[vc setPorts:SelectedPorts];

I also tried replacing [vc setPorts:SelectedPorts]; with vc.selectedCellIndexes = SelectedPorts; but the same problem occurs.

the setPorts method is declared in the FirstViewController.h file and SelectedPorts is a variable I declared in SecondViewController.m (it's not nil I checked).

Here's the setPorts: in FirstViewController.m :

- (void) setPorts:(id)selectedPorts {
selectedCellIndexes = selectedPorts;
NSLog(@"selectedCellIndexes : %@", selectedCellIndexes);
}

This logs the good value but when I log it in viewWillAppear in FirstViewController.m it's reset to the value it has before I called the method from SecondViewController.m.

Just to clarify, if I DON'T quit the SecondViewController.m, the data isn't reset.

I did read all your comments, and I really thanks you for your help. for convenience, I used a global variable.

Thanks for your help.

Upvotes: 4

Views: 4771

Answers (6)

karthika
karthika

Reputation: 4091

In secondViewController, You create protocol

  #import <UIKit/UIKit.h>

@protocol sampleDelegate <NSObject>

- (void)passValue:(id)selectedPorts

@end

@interface SecondViewController : UIViewController

@property (nonatomic, strong) id <sampleDelegate> passDelegate;

@end

In viewDidLoad or wherever method as per your need, call method like this,

[self.passDelegate passValue:selectedPorts];

In FirstViewController.h,

Import the delegate <sampleDelegate>,

#import "SecondViewController.h"

@interface FirstViewController : UIViewController<SampleDelegate>

@end

In FirstViewController.m,

-(void)passValue:(id)selectedPorts
{
    id receivedValues = selectedPorts;
}

and set self in your SecondViewController allocation,

SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
vc.passDelegate = self;

Upvotes: 1

St&#233;phane Bruckert
St&#233;phane Bruckert

Reputation: 22873

  • You have a list of ports in MasterViewController and you expect to use it in the SettingsViewController.
  • The MasterViewController can hold this list and SettingsViewController should have an access to it.

In SettingsViewController, have a setSelectedPort method:

@property (nonatomic, retain) id selectedPorts

- (void) setPorts:(id)selectedPorts;

The method saves the selected ports list into a property.

In MasterViewController, call the SettingsViewController and give it the list.

SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
[vc setSelectedPorts:yourValue]; 

When the list is modified inside the SettingsViewController, the list of ports contained in MasterViewController won't move even if you leave the SettingsViewController.

Upvotes: 1

Amol
Amol

Reputation: 85

This is problem related to object ownership. Follow the below steps:

As per understanding you want reverse value from "SecondViewController" to "FirstViewController"

Don't create new object of FirstViewController in SecondViewController, it will not work.

  1. Create object of "FirstViewController" in "SecondViewController.h" file. @property (nonatomic,strong) FirstViewController *firstViewController;

  2. When you navigate from FirstViewController to SecondViewController, please pass the "self". e.g. SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; vc.firstViewController = self;

  3. If you want pass the reverse value to FirstViewController then in SecondViewController.m file. [self.firstViewController setPorts:SelectedPorts];

  4. And in FirstViewController.m refresh your controls with latest values.

Try above code will defiantly work as per your requirement.

Upvotes: 0

user523234
user523234

Reputation: 14834

You were creating a new instance of the first view controller from the 2nd view controller not accessing the same instance of the original caller. That was the reason why while you could see the logs but data were not there when you got back to the original caller - your MasterViewController.

You need to use delegate method. Check my answer for this SO.

Upvotes: 0

ullstrm
ullstrm

Reputation: 10160

Either use delegate methods to communicate with the master VC from the modal VC, or you could do something like this if you want to retrieive some manipulated objects from the modal VC.

Set the object(s) as properties in the modal view controller's .h-file (so they are public).

Using unwind segues, in the master VC, just do this:

-(IBAction)exitModalVC:(UIStoryboardSegue*)segue
{
    SomeObject *obj = ((YourModalVC*)segue.sourceViewController).someObject;

    //Do what you want with obj
}

EDIT:

This will only work if you are using unwind segue (which is a neat way of dismissing modal VC when using story board)

And you are using this, which is not unwind segues:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];

Upvotes: 0

Anil Varghese
Anil Varghese

Reputation: 42977

There is nothing unusual in the getting result. By doing

MasterViewController *vc = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];   
[vc setPorts:SelectedPorts];

You are creating a new instance of MasterViewController from your SecondViewController. This is not the same from which you navigated to the SecondViewController. So you wont get the expected result. Since you are setting the ports([vc setPorts:SelectedPorts]) to the newly created instance of the Master.

Instead of creating a new instance,just hold the reference of the MasterViewController in SecondViewController in a property and assign it before moving to second VC. As a beginner I suggested this way. But using delegate is the prefferred way passing data back.

Upvotes: 0

Related Questions