views:

296

answers:

1

I have a table view that's showing a list of bank transactions. When I tap one, I'd like to show the editing dialog, initialized with the current data. I'm doing something like this currently to get the controller and to initialize the text fields on its view:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   
    TransactionDetailViewController *transactionDetailViewController;
    transactionDetailViewController = [[TransactionDetailViewController alloc] initWithNibName:@"TransactionDetailViewController" bundle:nil];

    // round up data
    NSMutableDictionary *rowDict = (NSMutableDictionary *)[transactions objectAtIndex:indexPath.row];

    transactionDetailViewController.amountField.text = [rowDict objectForKey:@"amount"];
    transactionDetailViewController.nameField.text = [rowDict objectForKey:@"name"];
    transactionDetailViewController.datePicker.date = [rowDict objectForKey:@"date"]; 

    [self.navigationController pushViewController:transactionDetailViewController animated:YES];
    [transactionDetailViewController release];
}

Unfortunately, this doesn't work, because the amountField, nameField, and datePicker haven't been initialized at this point -- they're 0x00, and as a result, get passed over when I try to set their text or date values. They do get properly initialized by the time viewDidLoad gets called. Do I have to have to store the row selected in the parent controller and then call back to the File Owner from the detail view, or is there some way I can actually initialize the detail view at the same time and place I create it?

+2  A: 

You have 2 options:

  1. Override the init method in TransactionDetailViewController and initialize the subviews there

  2. Pass the NSDictionary to TransactionDetailViewController and do your initialization in viewDidLoad.

I usually combine the two methods.

If your view is not complicated (and you don't really need to use an NIB file), you can construct your view in init.

On the other hand, passing the NSDictionary to the view might make it easier for you to get the return values.

So, in your case, I would probably add an NSDictionary property to TransactionDetailViewController, and do

[transactionDetailViewController setTransaction:rowDict]

Then in viewDidLoad, I would initialize the values in the subviews.

Can Berk Güder
Good deal! I was dinking around with it myself for a while, and figured out Option #2, which seemed a nice clean way to do it. Thanks for the direction!
Sean McMains
You're welcome. =)
Can Berk Güder