-5

Is it possible to send data from one ViewController to another ViewController where the second ViewController is embedded in a Navigation Controller? (and vice versa)

NOTE: sorry i missed should have aded more details. The problem here is that the app structure goes like this: ViewControllerOne >UINvaigationController> ViewControllerTwo. So i want to pass the data to ViewControllerTwo. i tried to pass it through PrepareForSegue but i receive this error:

-[UINavigationController setString:]: unrecognized selector sent to instance 0x98b71c0

Here is the code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
   VCC *vc2 = (VCC*)segue.identifier;
   vc2.string = @"something";
}
user1938695
  • 623
  • 1
  • 5
  • 14

4 Answers4

1

An example to pass data in your case is this :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"segueName"]) {

    SecondClassName *destTableViewController = [[segue.destinationViewController viewControllers] objectAtIndex: 0];
    destTableViewController.yourDataInSecondClass=yourDataInFirstClass;

}}

Care: You must give a name to the segue that connects the First VC with the Navigation Controller and change the segueName in the code above with this name.

prepareForSegue method is being called by own every time that you choose to change VC.

hoya21
  • 893
  • 7
  • 24
  • thanks. i tried this didn't work for my case. Please see the note in my question that i just added. – user1938695 Nov 21 '13 at 11:00
  • Can you redit your post and add some code? – hoya21 Nov 21 '13 at 11:07
  • If you use Storyboard you have to select the segue that you want and give it a name in Attribute Inspector. Then use exactly the code that i gave you and change the "segueName" with this that you have given before. And replace "SecondClassName" with your second VC name. – hoya21 Nov 21 '13 at 11:15
0

Ofcourse. Save the viewcontroller references some where (not the best solution). For example in the AppDelegate, [[UIApplication sharedApplication] delegate].viewController1 and [[UIApplication sharedApplication] delegate].viewController2

It depends on your structure how to obtain the viewcontrollers, but its not hard.

To get all viewcontrollers within a navigationController: self.navigationController.viewControllers. It returns are NSArray with ViewControllers.

Leon Boon
  • 357
  • 3
  • 16
0

Actually there are lots of ways for this query -

Solution from one of them -

In your FirstViewController -

On UIButton action -

SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.stringForPassing = @"PassString";
[self.navigationController pushViewController:secondViewController animated:YES];

In SecondViewController.h

@property (nonatomic,copy)NSString *stringForPassing;

In SecondViewController.m

@synthesize stringForPassing;

Now you can access stringForPassing in SecondViewController.

May this will help you. Thank you in advance.

iSmita
  • 1,292
  • 1
  • 9
  • 24
  • This is not working if you have already an existing secondViewController, because with this you create a new instance of SecondViewController – Retterdesdialogs Nov 21 '13 at 10:49
  • Thanks. i know this way and i did something similar (since i'm using segues), i took the segue.destination and created a property is the second VC however i get an error that i'm sending an instance to UINavigationController instead of the destination Vc. Note: there's a UINavigation controller between the first and second VC – user1938695 Nov 21 '13 at 10:49
  • 1
    @Retterdesdialogs and user1938695 - Thank you for reply.I will post another one.:) – iSmita Nov 21 '13 at 10:51
0

you can simply use properties to send data between controllers .

let say you have 2 Viewcontrollers , FirstViewController & SecondViewController .and you wanted to send a string @"hello world" to SecondViewController

then

1. create a @property(nonatomic,retain) NSString * nameStr ; in SecondViewController 

2. synthesize it .

3. suppose your navigating to secondViewController on click of a button .

-(void) onButtonClicked
{
     SecondViewController * secondVC = [[SecondViewController alloc] init];

     secondVC.nameStr = @"hello , world";

     [self.navigationController pushToViewController:secondVc];
}

now you can get the value in SecondViewController by calling self.nameStr

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70