To follow up on what's already been said, one approach commonly taken in similar problems is to make the ItemViewController
(parent) the delegate of CategorySelectionViewController
(child), and when tableView:didSelectRowAtIndexPath:
fires in the CategorySelectionViewController
, send a message to the delegate callback in ItemAddViewController
- passing in the selected category as a parameter.
This concept could be implemented similar to the following:
@protocol CategorySelectionViewControllerDelegate;
// in CategorySelectionViewController.h
@interface CategorySelectionViewController : UITableViewController {
id<CategorySelectionViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id<CategorySelectionViewControllerDelegate> delegate;
@end
@protocol CategorySelectionViewControllerDelegate
// delegate callback skeleton
-(void)userDidSelectCategory:(Category *)categorySelected;
@end
// in CategorySelectionViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *currentCategory = category;
if (currentCategory != nil) {
NSInteger index = [categories indexOfObject:currentCategory];
NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
checkedCell.accessoryType = UITableViewCellAccessoryNone;
}
//set the checkmark accessory
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
// here's where you message the delegate callback
[self.delegate userDidSelectCategory:[categories objectAtIndex:indexPath.row]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
ItemAddViewController's skeleton would then be modified to conform to the CategorySelectionViewControllerDelegate
protocol:
// in ItemAddViewController.h
@protocol CategorySelectionViewControllerDelegate;
@interface ItemAddViewController : UITableViewController <CategorySelectionViewControllerDelegate>
{ /* etc.... */ }
@property (nonatomic, retain) Category *category;
// delegate callback
-(void)userDidSelectCategory:(Category *)categorySelected
// in ItemAddViewController.m
// set the CategorySelectionViewController delegate as this ItemViewController when you instantiate it
-(void)showCategorySelectionViewController {
CategorySelectionViewController *myChild = [[CategorySelectionViewController alloc] init];
myChild.delegate = self;
[self presentModalViewController:myChild animated:YES];
}
// implement the delegate callback
-(void)userDidSelectCateogry:(Category *)categorySelected {
self.category = categorySelected;
// other handling code as needed...
}
In regard to doing this by calling [self parentViewController]
in CategorySelectionViewController
, the catch is that ItemAddViewController
inherits from UITableView
, so when you send the message [self parentViewController]
, the compiler thinks you're talking to a UITableView
, not an ItemAddViewController
, unless you cast it explicitly. Therefore, it does not know that self.parentViewController
has a property called category
. You can fix this by adding the type cast:
ItemAddViewController *itemAddViewControllerParent = (ItemAddViewController *)[self parentViewController];
itemAddViewControllerParent.category = [categories objectAtIndex:indexPath.row];
Hope this helps.