Zeel
Zeel

Reputation: 234

Passing data from one view controller to other view controller through xib file

I want to make one app and it include form submition . In this i create one new project by single view application. In this firstViewController I have put button and by pressing it , it redirect to secondViewController.

In this i have a form. and want to take data from user and by submit button i want to redirect this to thirdViewController and want to print the form data on thirdViewController.

In firstViewController.m file i have written

-(IBAction)nextVCButton:(id)sender
{

SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

NSLog(@"name : %@",self.nameTextField.text);
tempView.fullName = self.nameTextField.text;
[tempView setFullName:self.fullName];
[self.view addSubview:tempView.view];

}

In SecondViewController.m file

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

NSLog(@"received info %@", [self.fullName description]);

self.nameLabel.text = [self.fullName description];

}

In log i am getting (name :) value but in secondViewController received info is null.

I am doing this all by using xib file. No use of Storyboard.

Please help me to get this form data to secondViewController.

Thanks.

Upvotes: 2

Views: 2979

Answers (3)

kulss
kulss

Reputation: 2055

I think you need to use Navigation controller if you want to navigate from one view controller to another view controller. well here you need to do property- synthesize the iVar "fullName" in ThirdViewController. and retain the value in SecondViewController .

1. ThirdViewController.h

 @property(nonatomic, retain) NSString *fullName;

2. ThirdViewController.m

@synthisize fullName;
dealloc {
[fullName release];
}

3. SecondViewController.m

-(IBAction)nextVCButton:(id)sender
{

SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

NSLog(@"name : %@",self.nameTextField.text);
tempView.fullName = self.nameTextField.text; 
[tempView setFullName:self.fullName];
[self.view addSubview:tempView.view];
}

Upvotes: 1

V.J.
V.J.

Reputation: 9580

try presentViewController method instead of addSubview....

-(IBAction)nextVCButton:(id)sender
{
    SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    NSLog(@"name : %@",self.nameTextField.text);
    tempView.strFullName = self.nameTextField.text;
    [self.view addSubview:tempView.view];
    [self presentViewController:tempView animated:YES completion:nil];
}

Upvotes: 0

Mansi Panchal
Mansi Panchal

Reputation: 2357

Sorry , I couldn't get why you have done these two line code :

tempView.fullName = self.nameTextField.text;
[tempView setFullName:self.fullName];

Instead , Make a property of NSString in Second View Controller's .h file

@property (nonatomic,retain)    NSString *strFullName;

and synthesize in .m file.

@synthesize strFullName;

Now ,

-(IBAction)nextVCButton:(id)sender
{

SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

NSLog(@"name : %@",self.nameTextField.text);
tempView.strFullName = self.nameTextField.text;
[self.view addSubview:tempView.view];
}

Upvotes: 2

Related Questions