views:

51

answers:

4

I want to load data (an array of strings) from the parent view into a set of UITextFields in the child view upon presenting the modalView.

I know how to pass from child to parent, and I'm sure it's even easier to go the other way, but I don't know how.

Thanks in advance!

UPDATE: Update removed because I found the problem (double releasing of modal view)

+1  A: 

Override the init method for the child view controller.

- (id) initWithStrings:(NSArray *)string {
    if (self = [super init]) {
        // Do stuff....
    }
    return self;
}

Then in the parent:

MyChildViewController *vc = [[[MyChildViewController alloc] initWithStrings: strings] autorelease];
Matt Williamson
Thanks! Worked great, but two problems have come up.1. The parent .m file is giving me a warning No '-initWithStrings:' method found. Where do I need to define it?2. Once the child view has loaded, and I've used viewDidLoad to populate my UITextFields, when I tap on any other UI Elements (buttons, text fields) the app freezes and I get "sharedlibrary apply-load-rules all(gdb)" in my console and my Thread 1 has lots of references to "prepareForMethodLookup"Thoughts?
Collin Mason
Put it in the .h file too lik so: - (id) initWithStrings:(NSArray *)string;
Matt Williamson
Not sure about #2, did you get EXC_BAD_ACCESS?
Matt Williamson
#1 fixed: for some reason when I tried that the first time, no dice.#2 is still a problem though. I don't get a EXC_BAD_ACCESS. I've updated the original question with a copy of the dump I'm getting.
Collin Mason
I fixed it! I had the autorelease you had given me, but then manually released it at the end of the IBAction. Seems to work just how i wanted, thanks again!
Collin Mason
Ah yes, that'd do it. Glad it's working.
Matt Williamson
A: 

Two ways you could do it:

1.Override the init method as Matt suggests

2.Create fields in your child class and pass those values to your text field.

@interface ChildViewController : UIViewController{
    NSArray *strings;
    UITextfield *textField1;
    UITextfield *textField2;
}
...

- (void)viewDidLoad {
    [super viewDidLoad];
    textField1.text = [strings objectAtIndex:0];
    textField2.text = [strings objectAtIndex:1];
}

Then in the parent class:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChildViewController *childController = [[ChildViewController alloc] init];
    childController.strings = your_array_of_strings;
    [self.navigationController pushViewController:childController animated:YES];
    [childController release];

}
domino
A: 
- (id)initWithDataObject:(YourDataObjectClass *)dataObject {
    if (self = [super init]) {
        self.dataObject = dataObject;
        // now you can do stuff like: self.myString = self.dataObject.someString;
        // you could do stuff like that here or if it is related to view-stuff in viewDidLoad
    }
    return self;
}
Fossli
A: 

If you want to get really fancy, you can make a delegate for your child view.

@protocol MyChildViewDelegate
- (NSArray*)getStringsForMyChildView:(MyChildView*)childView;
@end

@interface MyChildView : UIView
{
    id <MyChildViewDelegate> delegate;
    ...
}

@property (nonatomic, assign) id <MyChildViewDelegate> delegate;
...
@end

Then somewhere in your view you would ask for the strings:

- (void)viewDidLoad
{
    ...
    NSArray* strings = [delegate getStringsForMyChildView:self];
    ...
}

Then in your controller (or where ever) you can do:

myChildView = [[MyChildView alloc] initWith....];
myChildView.delegate = self;

...

- (NSArray*)getStringsForMyChildView:(MyChildView*)childView
{
    return [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
}

It's probably a little overkill in this case, but this is how UITableViews do it too: they have a data source delegate to provide them with their contents.

Hollance