Maurizio
Maurizio

Reputation: 89

Assign value to textfield firstview take of secondview

I have two views in my application: in main view there are input fields that the user must fill and a button connected to the second view. In the second view there is a table view, when the user selects a row automatically returns to the main view. My problem is that when you return to the main view the values ​​of text fields are cleared. Any solutions? Thank you.

Second View header

    @class MainViewController;

    @interface ListaViewController : UIViewController 
    <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>
    {
       UITableView *table;
       UISearchBar *search;
       MainViewController *child;
    }

    @property (nonatomic, retain) IBOutlet UITableView *table;
    @property (nonatomic, retain) IBOutlet UISearchBar *search;
    @property (nonatomic, retain) MainViewController *child;


    - (IBAction)switchBack:(id)sender;

Second View Implementation:

      -(IBAction)switchBack:(id)sender 
      {
        child.selectedCountry = selectedCountry;
        child.codiceComune = codiceComune;
        [self.navigationController popViewControllerAnimated:YES];
      }

First View:

      -(void)viewDidAppear:(BOOL)animated 
      {
        [super viewDidAppear:animated];
        cityField.text = selectedCountry;
      }

This not work!

Upvotes: 0

Views: 242

Answers (2)

Padavan
Padavan

Reputation: 1074

 -(IBAction)switchBack:(id)sender 
      {
         MainViewController *controller = [[MainViewController alloc]  initWithNibName:@"MainViewController" bundle:nil];
        controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:controller animated: YES];
        [controller release];
      }

It means you create new MainViewController, not return to previous.

Use this code instead

-(IBAction)switchBack:(id)sender  
{
    [self dismissModalViewControllerAnimated:YES];
}

P.S. It works if you go to the second vievcontroller same way, as you write in -(IBAction)switchBack:(id)sender

Upvotes: 1

ader
ader

Reputation: 5393

As a guess without code, you're probably emptying or initialising your text fields in viewDidLoad, viewDidAppear or viewWillappear - do it in your viewController init instead.

Upvotes: 0

Related Questions