Reputation: 59203
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?
Upvotes: 0
Views: 666
Reputation: 113310
You have 2 options:
Override the init
method in TransactionDetailViewController
and initialize the subviews there
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.
Upvotes: 2