1

I thought this would be a no brainer, but it turns out I have spent about 5 hours on this now.

I have two ViewControllers and I want to pass a pre formatted NSString to another VC, using a IBAction called putInfo. All this action is responsible for is putting a word into a label on another VC, .

so, in the first ViewController, I implemented the code like this :

- (IBAction)putInfo:(id)sender {

((secondViewController *)self.presentingViewController).ouputLabel.text = @"chicken";

}

I have tried other things like-grabbing a reference to the second VC, instantiating the second view controller, doing that thing where you initialize the second VC WithNibName--all that. ABove is just my latest failure.

This seems like it should be such a no brainer. any suggestions?

RunnerGirl
  • 53
  • 6
  • You should take a look at this post. http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Jean-Francois Gagnon Feb 27 '13 at 18:11
  • ok--I have already tried the first solution and got lost so, I am trying the next one that uses controller a and b as examples. in the first part where it has @interface viewControllerB : UIViewController { NSString *string; NSArray *array; } - (id)initWithArray:(NSArray)a andString:(NSString)s;...if I am only using a string, would I put - (id)initWithString:andString:(NSString)s; in the interface file? BTW-I don't know where the code input icons went for this text box. – RunnerGirl Feb 27 '13 at 18:21
  • no, that didn't work either--this code is very old. – RunnerGirl Feb 27 '13 at 18:37

1 Answers1

0

If you're using Storyboard, you'll need to use the method prepareForSegue. But first you need to put a name to your segue: click on it, go to Attribute Inspector and write an Identifier name for the segue.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier]isEqualToString:@"mySegue"])
{
    secondViewController* second = segue.destinationViewController;
    second.outputLabel = txtFieldSecondViewController;
}

}

You pass the value of a textField(or some string) to another NSString object of the second view.

Gustavo Barbosa
  • 580
  • 3
  • 13
  • ok--I will try that thanks. Do you know if I can set the text in performSegueWithIdentifer part? I am trying to set the text when the user presses the button on the first VC. so, I had tried this.....- (IBAction)putInfo:(id)sender { [self performSegueWithIdentifier:@toVerbChooser" sender:sender:} and I was trying to say something like outputLabel = @"chicken" in that method, but that didn't work either. Sorry, I cant find insert code button and when I hit return key, the message sends. – RunnerGirl Feb 27 '13 at 18:48
  • Make a button IBAction with a simple code: [self performSegueWithIdentifier:@"mySegue" sender:nil]; and the segue will get the value of the string... – Gustavo Barbosa Feb 27 '13 at 19:33