views:

808

answers:

3

I know this question has been asked several times and I did read existing posts on this topic but I still need help.

I have 2 UIViewControllers - Parent and Child. I display the child view using the presentModalViewController as below:

ChildController *child = [[ChildController alloc[ initWithNibName:@"ChildView" bundle:nil];
[self presentModalViewController:child animated:YES];
[child release];

The child view has a UIPickerView. When user selects an item from UIPickerView and clicks done, I have to dismiss the modal view and display the selected item on a text field in the parent view.

In child's button click delegate, I do the following:

ParentController *parent = (ParentController *)[self.navigationController parentViewController];
[parent.myTextField setText:selectedText];
[self dismissModalViewControllerAnimated:YES];

Everything works without errors. But I don't know how to load the Parent View so that it displays the updated text field.

I tried

[parent reloadInputViews];

doesn' work. Please help.

A: 

If a low-memory warning comes in during your modal view's display, the parent's view will be unloaded. Then parent.myTextField is no longer referring to the right text field until the view is reloaded. You can force a reload of the view just by calling parent.view;

However, a better idea might be to have the parent view have a String property that can be set by the child view. Then, when the parent view reappears, put that data into the text field, inside viewWillAppear: for example. You'd want to have the value set to some default value for when the parent view initially shows up too.

Ed Marty
viewWillAppear doesn't fire when child modal is dismissed. The 'Done' button is inside the child view where i get the picker view's selected text and then dismiss the modal. the parent is just there and doesn't have to re appear. i am guessing thats why it doesn't fire viewWillAppear.
Dave
If you're doing everything correctly, using the framework the way it's supposed to be used, viewWillAppear should be called. Are you sure you are overriding the right method in the right class?
Ed Marty
A: 

Can't you just send a -viewWillAppear: message yourself just before dismissing the modal view controller?

jlehr
it doesn't work for some reason. I put a break point. Doens't get hit.
Dave
+1  A: 

Delegation is the way to go. I know some people that may be looking for an easier solution but trust me I have tried others and nothing works better than delegation. So anyone having the same problem, go read up on delegation and follow it step by step.

In your subviewcontroller.h - declare a protocol and declare delegate mthods in it.

@protocol myDelegate
 -(void)clickedButton:(subviewcontroller *)subController;
@end

In your subviewcontroller.h, within @interface:

id<myDelegate> delegate;
@property (nonatomic, assign) id<myDelegate> delegate;    
NSString *data;
-(NSString *)getData;

In your subviewcontroller.m, synthesize myDelegate. Add the following code to where you want to notify your parentviewcontroller that the subview is done doing whatever it is supposed to do:

 [delegate clickedButton:self];

and then handle getData to return whatever data you want to send to your parentviewcontroller

In your parentviewcontroller.h, import subviewcontroller.h and use it's delegate

 #import "subviewcontroller.h"
 @interface parentviewcontroller : VUIViewController <myDelegate>
 {}

In your parentviewcontroller.m, implement the delegate method

 - (void)clickedButton:(subviewcontroller *)subcontroller
 {
   NSString *myData = [subcontroller getData];
   [self dimissModalViewControllerAnimated:YES];
   [self reloadInputViews];
 }

Don't forget memory management!

Dave