0

I am a bit stuck on trying to pass data from two UITextfields on one viewcontroller to another viewcontroller. Basically I got the items below:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    IBOutlet UITextfield *Textfield1;
    IBOutlet UITextfield *Textfield2;
}

-(IBAction)GoNextView:(id)sender;

@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

(IBAction)GoNextView:(id)sender {

    SecondView *second = [[SecondView alloc] initWithNibName:nil bundle:nil];

    [self presentViewController:second animated:YES completion:NULL];  
}

- (void)viewDidLoad {
     [super viewDidLoad];
}

SecondView.h

#import <UIKit/UIKit.h>

@interface SecondView : UIViewController {

    IBOutlet UILabel *ShowTextfield1;
    IBOutlet UILabel *ShowTextfield2;
}

-(IBAction)GoBack:(id)sender;

@end

SecondView.m

@interface SecondView ()

@end

@implementation SecondView

- (IBAction)GoBack:(id)sender {

    [self dismissViewControllerAnimated:YES completion:NULL];
}

What I want is, that when the button GoNextView is pressed it should pass the data in the UITextfield to the SecondView. And the go to the next page. The data should now be displayed in the two Labels on the SecondView. I've been struggling with this for many hours now and its killing me. I know I have to use some NSString and @property(nonatomic, retain) NSString *asdas. But I simply can't make it work. So based on the above code how do I pass data from the two UITextfield to the two labels on the next view?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hemang Jan 23 '16 at 12:22

2 Answers2

0

Instead of navigating to the second view through a function, use Storyboard Segues. Create a storybaord application. To Create a segue you need a navigation view controller. You can do that by the following steps.

  1. Select the first view controller and go to Editor -> Embed In -> Navigation Controller.
  2. Then Ctrl+Click on the "GoNextView" button, and drag and drop in the SecondView.
  3. You will get three options: push, model, custom. Click push then a segue will be created between these two views.
  4. Click that segue and click the "Show the Attribute Inspector" on the right side property window. Now name it as "connector" or which ever name you prefer.

Now, in SecondView.h create two strings,

@property (nonatomic, retain) NSString *str1;
@property (nonatomic, retain) NSString *str2;

and synthesize them in SecondView.m.

Lets name your UITextFields as "text1" and "text2" which are in your ViewController. Go to the ViewController.h and add #import "SecondView.h"

In ViewController.m do the following,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"connector"])
    {
        ViewController *VC = [segue destinationViewController];

        VC.str1 = text1.text;
        VC.str2 = text2.text;
    }
}

Now you can display these strings in your SecondView. In Secondview.m,

label.text = str1;

and it will be displayed.

Gravity M
  • 1,485
  • 5
  • 16
  • 28
0

Use NSUserDefaults or the normal synthesising common interface variables. For example,

  1. NSUserDefaults From sender's view controller,

    NSString *text=textField1.text; NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults]; [prefs setObject:text forKey:@"text1value"];

And you can get the value from any view controller as,

NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];
NSString *received string= [prefs stringForKey:@"text1value"];
  1. Normal assigners,

I believe you must be using performsegue to navigate view controllers. There is a delegate method called "prepareforsegue" and you can assign the textfield values into the interface variables. For example,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"seguename"]) {
        destViewController *destViewController = segue.destinationViewController;
        destViewController.name = @"some text";
    }
}

In the receiver's view controller, getter variable is actually defined as,

In .h file, @property(nonatomic, strong) NSString *name;

In .m file, Don't forget to synthesis the variable after implementation

@implementation destViewController
@synthesize name;

Hope it helps!

  • This is very very bad. There's always a good solution for a problem. Can't we use @properites or a setter method? – Hemang Jan 23 '16 at 12:23
  • @Hemang Yes, we can!! But, in terms of simple variable sharing, we can use NSUserDefaults. However, I've mentioned that method also by editing my answer – Pradheep Narendran P Jan 23 '16 at 12:47
  • I removed my down vote, but still I strongly believe you should only suggest this by using setter or property only. – Hemang Jan 23 '16 at 12:48