1

I'd like to pass through the data entered into the UITextFields in the current view controller to a second view controller on the save button action. I'm using storyboards, could anyone advise me on how to achieve this? I'm using Xcode 6.

@IBAction func SAVEButtonTapped(sender: AnyObject) {

    let viewcontroller = SecondViewController();
    viewcontroller.name = self.nametextfield.text;
    viewcontroller.phone = self.phonetextfield.text;
    viewcontroller.city = self.citytextfield.text;

}
Ramkumar chintala
  • 958
  • 11
  • 24

4 Answers4

2

I found my Problem

@IBAction func SAVEButtonTapped(sender: AnyObject) {

let viewcontroller = SecondViewController();
viewcontroller.name = self.nametextfield.text;
viewcontroller.phone = self.phonetextfield.text;
viewcontroller.city = self.citytextfield.text;

}

problem with this line let viewcontroller = SecondViewController()

i changed it to

let viewcontroller = self.storyboard!.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondviewController

With This change My code working fine thanks..

Ramkumar chintala
  • 958
  • 11
  • 24
0

Try this in FirstViewController,

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

    UIViewController *destinationVC = [segue destinationViewController];
    If ([destinationVC isKindOfClass:[SecondViewController class]]) {

        SecondViewController *viewcontroller = (SecondViewController *)destinationVC;
        viewcontroller.name = self.nametextfield.text;
        viewcontroller.phone = self.phonetextfield.text;
        viewcontroller.city = self.citytextfield.text;
    }
}

Assuming that you have only one segue to SecondViewController. If you have multiple segue to SecondViewController then you can check segue identifier instead of isKindOfClass check.

Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
0

What you are doing will not work because your views will not be initialized until viewDidLoad: is executed. Basically, you're assigning values to nil objects.

You either need to add string properties that you later assign to your view elements in viewDidLoad or do what Akhilrajtr mentioned if you are using a segue for your transition.

Edward L.
  • 564
  • 2
  • 9
0

pass the data between VC create a intent for destination VC,like below

SecondViewController *controller = [[SecondViewController alloc]  initWithNibName:@"SecondViewController" bundle:nil];
    viewcontroller.name = self.nametextfield.text;
    viewcontroller.phone = self.phonetextfield.text;
    viewcontroller.city = self.citytextfield.text;
 [self.navigationController pushViewController:controller animated:YES];
Dharma
  • 3,007
  • 3
  • 23
  • 38
  • 1
    i am tried your code but it shows ibc++abi.dylib: terminating with uncaught exception of type NSException – Ramkumar chintala Dec 16 '14 at 12:31
  • but it's worked for me.i don't know exact reason check the file's owners,if yellow triangles are shown click the cross (X) to remove the yellow triangles. – Dharma Dec 16 '14 at 13:09