2

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.

Zeel
  • 236
  • 5
  • 15
  • using addsubview to load second view controller?seriously?Then why `UINavigationController` class being made? – Lithu T.V Jun 17 '13 at 05:47
  • http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Solid Soft Jun 17 '13 at 05:57
  • which way is more preferable for navigate one view to another by button and again from that controller navigate to third controller through button? – Zeel Jun 17 '13 at 11:26

3 Answers3

2

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];
}
Mansi Panchal
  • 2,357
  • 18
  • 27
  • Thank you so much.. I have already done same thing but because of this line [tempView setFullName:self.fullName]; I am getting null. Now after removing this it is working fine. I spend my 2 days for this. thanks for your valuable answer. – Zeel Jun 17 '13 at 06:02
  • which way is more preferable for navigate one view to another by button and again from that controller navigate to third controller through button? – Zeel Jun 17 '13 at 11:27
  • It can be according to the requirement of your application. – Mansi Panchal Jun 18 '13 at 04:28
  • for this same question i am using both times addSubView for navigate to another page. By pressing button in second view controller it is giving error of " Thread 1 : EXC_BAD_ACCESS(code=1, address=0x69202020) " – Zeel Jun 18 '13 at 04:58
  • But if you want to move to second view then you need to use pushViewController , else if you want to add that second view on first view , then you need to add subview on first view. – Mansi Panchal Jun 18 '13 at 05:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31907/discussion-between-zeel-and-iuser) – Zeel Jun 18 '13 at 05:24
1

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];
}
kulss
  • 2,057
  • 1
  • 14
  • 16
0

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];
}
V.J.
  • 9,492
  • 4
  • 33
  • 49