0

I have two views with navigation controller: first view, there are empty text fields while the second view is a table view where the user can select a row. At the touch of a row there is an action that sets a value to a text field of the first view. Unfortunately when I go back to the first view field is not set.

This is my code:

FirtViewController.h

      @interface FirstViewController : UIViewController 
      {
          UITextField *firstField;
          UITextField *secondField;
      }
      @property(nonatomic, retain) IBOutlet UITextField *firstField;
      @property(nonatomic, retain) IBOutlet UITextField *secondField;
      @property(copy) NSString *selectedRow;
      -(IBAction)showTable:(id)sender

FirstViewController.m

      #import "FirstViewController.h"
      #import "AppDelegate.h"
      #import "SecondViewController.h"

      @implementation FirstViewController
      @synthesize  .....
      .............


      -(void)viewDidAppear:(BOOL)animated 
      {
        [super viewDidAppear:animated];
        self.firstField.text = selectedRow;
      }

      -(IBAction)showTable:(id)sender
      {
         SecondViewController *controllerSecond = [[SecondViewController alloc]  initWithNibName:@"SecondViewController" bundle:nil];
         [self.navigationController pushViewController:controllerSecond animated:YES];
      }

SecondViewController.h

      @class FirstViewController;

      @interface ListaViewController : UIViewController 
      <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
      {
         UITableView *table;
         UISearchBar *search;
         FirstViewController *controller;
      }
      @property (nonatomic, retain) FirstViewController *controller;

SeconViewController.m

      - (void)tableView:(UITableView *)tableView 
         didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

            NSString *selectedRow = [tableData objectAtIndex:indexPath.row];

            controller.selectedRow = selectedRow;
            [self.navigationController popViewControllerAnimated:YES];
      }
Maurizio
  • 89
  • 2
  • 15

4 Answers4

1

Based on your code above, you never set the connection in secondViewController back to first
You probably need something like this:

  -(IBAction)showTable:(id)sender
      {
         SecondViewController *controllerSecond = [[SecondViewController alloc]  initWithNibName:@"SecondViewController" bundle:nil];
         [controllerSecond setController:self];  //this sets the reference back
         [self.navigationController pushViewController:controllerSecond animated:YES];
      }
Walter
  • 5,867
  • 2
  • 30
  • 43
  • Received this warning: setController not found – Maurizio Dec 23 '11 at 17:12
  • I have try with: controllerSecond.FirstViewController = self; but not work – Maurizio Dec 23 '11 at 17:28
  • Make sure you @synthesize the controller in the SecondViewController. This answer should work, if you synthesize the FirstViewController property in SecondViewController. Or you could use a Notification to send the data back to the FirstViewController. – Hubert Kunnemeyer Dec 23 '11 at 17:30
  • Sorry, I had assumed that you had #include and @synthesize all set up at the top of secondviewcontroller.m – Walter Dec 23 '11 at 17:44
0

I'm not certain I fully understand what you mean, but why don't you creating an NSString containing the value you need in the SecondViewController. That way you can do something like this when you set up the SecondViewController.

SecondViewController *nextView = [[SecondViewController alloc] init];
nextView.myNSString = @"Value you need in the next view";

Then in SecondViewController.h you'll be able to access the NSString like so:

@NSLog(@"%@", self.myNSString);
  • I have to take the value of the table in the second view and set it on a text field of the first view – Maurizio Dec 23 '11 at 17:02
0

The main thing you are missing is the Value passing between views. You are not using any object in any of the view that is passing the value to another view. Like, If you want to pass a text from FirstViewController to SecondViewController. You Can do it as follwos.

NSString *textToPass = self.firstField.text;

In Your SecondViewController there need to be a string object say passedText. When you create object of secondViewController in firstViewController, Do it as follows:

SecondViewController *controllerSecond = [[SecondViewController alloc]  initWithNibName:@"SecondViewController" bundle:nil];
controllerSecond.passedText = textToPass;
[self.navigationController pushViewController:controllerSecond animated:YES];

Now Wherever you want to show the text of first screen, just use "passedtext" String.

Hope it Helps. :)

===============================================

In your code you may try the followinf modification:

NSString *selectedRow = [tableData objectAtIndex:indexPath.row];
            controller = (FirstViewController *)[self.navigationController topViewController];
            controller.selectedRow = selectedRow;
            [self.navigationController popViewControllerAnimated:YES];

It Will resolve your problem forsure.

iCreative
  • 1,499
  • 1
  • 9
  • 22
0

What you need to use is delegate. It is very commonly used pattern in Object-C. Check out my answer to this SO post. Let me know if you still need code after.

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102