1

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.

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Collin Mason
  • 17
  • 1
  • 5

4 Answers4

2

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
  • 39,165
  • 10
  • 64
  • 72
  • 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 Aug 10 '10 at 16:37
  • Put it in the .h file too lik so: - (id) initWithStrings:(NSArray *)string; – Matt Williamson Aug 10 '10 at 17:04
  • Not sure about #2, did you get EXC_BAD_ACCESS? – Matt Williamson Aug 10 '10 at 18:22
  • #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 Aug 10 '10 at 19:07
  • 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 Aug 10 '10 at 19:51
0

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
  • 2,137
  • 1
  • 22
  • 30
0
- (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;
}
hfossli
  • 22,616
  • 10
  • 116
  • 130
0

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
  • 2,958
  • 16
  • 15