views:

161

answers:

3

an Hi,

I have an UITextField in one view, UITextField A in View A. And I have another in view B, UITextField B in View B.

I use a Navigation Controller Bar to switch between the views. The UITextFields are properties and connected Outlets of both views A and B.

On my view A there is an "Options"-button which pushes view B. So when you are typing in Textfield A, I would like the same text to appear in Textfield B.

When you edit TextField B and you go back to view A (via the "Title View A"-button in de navigation bar), I would like the same text to re-appear in Textfield A.

I expected this to be easy. But I can't get it working.

I tried:

ViewBController *controller = [[ViewBController alloc] initWithNibName:@"ViewBController" bundle:nil];
controller.TextFieldA.text = TextFieldB.text

But nothing appeared in view B. And how do I get back? I dont want to use NSUserdefaults because I would have to remove the values also.

A: 

The problem here might be that when u try setting textfieldAs text it is very possible that textFieldA has not been instantiated yet....you should check to see that it has...but i wouldnt connect textFieldA through IB because with your method here when ur view appears it might reintialize its Outlets and clear the text as well, i would probably programitcally set the textfield...hope this helps

Daniel
A: 

Think Model View Controller (MVC)...NSUserDefaults could be a place to store your model, but so could an NSString

Lets assume you go with the NSString. Create it in A & make it a property of B, update your model (the string) in each view in viewWillDisappear. Update the view from the model in ViewWilAppear. Job done.

EDIT: To update a property of B

MyVC * b = [[MyVC alloc] init]; b.myProperty = theReallyImportantValueString;

Then display b with push or displayModalViewController

Andiih
OK. But how do I update a property of another viewcontroller in this case? I guess I also have to release the string immediately after setting the property.
Chrizzz
No! Not at all. Your model needs to persist - so create it at the top level thing that persists for ever. I'm guessing from your description that is 'A'. Release it in A's dealloc (but in practice that will only be called as you exit the app, so it's irrelevant).This really isn't compicated, so if its not working, insert NSLog statements to figure out what is being called, and in what order. A's ViewWillDisappear should update your (rather simplified) model. B's viewWillAppear should read the model and write it to the screen. Then visa-versa. I've added somee code.
Andiih
Thank you Andiih. Your answer was very helpful. It "pushed" me in the right direction. As the full answer was a bit more complicated I will post it seperately.
Chrizzz
A: 

Thanks to Andii I finally found it.

First of all you can not push UITextFields properties to another view. I had to make a separate NSString that could be pushed. Like this

    BViewController *bViewController = [[BViewController alloc] initWithNibName:@"BViewController" bundle:nil];
    bViewController.aString = self.aTextField.text;

In the viewWillAppear of B I put:

bTextField.text = aString;

Then I wanted to push back the bTextField.text to the A view. I had to make a viewWillDisappear like this (you can also write it shorter):

- (void) viewWillDisappear:(BOOL)animated  {
NSArray *viewControllerArray = [self.navigationController viewControllers]; 
int parentViewControllerIndex = [viewControllerArray count] - 1;

[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBString:bTextField.text];

 [super viewWillDisappear:animated];

}

Finally I had to add this in the viewWillAppear of A:

    if ([self.bString length] > 0) {
    aTextField.text = self.bString;
}
Chrizzz