In RootViewController
I have a button which when it's clicked makes the self.navigationController
push to CategoryViewController
.
Then, in CategoryViewController
, click one cell push to SubCatsViewController. and When I have chosen a cell, and it should dissmiss CategoryViewController
and SubCatsViewController
back to the RootViewController
.
But how to do that?
If I use dismissViewControllerAnimated, it only dismisses SubCatsViewController
, not CategoryViewController
. I have wrote a delegate in SubCatsViewController
so I can get the selected value from SubCatsViewController
, and in RootViewController
I conform to this delegate protocol, to get the value that I wanted.
However, I can't get the value using the delegate I have written.
- (void)chooseCat:(BButton *)sender
{
UIStoryboard *storyboard = [UIStoryboard
storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
CategoryViewController *cat =
[storyboard instantiateViewControllerWithIdentifier:@"Cats"];
SubCatsViewController *sub =
[storyboard instantiateViewControllerWithIdentifier:@"SubCats"];
sub.delegate = self; //correct way?
[self.navigationController pushViewController:cat animated:YES];
}
CategoryViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
UIStoryboard *storeboard =
[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
SubCatsViewController *sub =
[storeboard instantiateViewControllerWithIdentifier:@"SubCats"];
sub.subCats =
[[self.cats objectAtIndex:indexPath.row] objectForKey:@"subcat"];
[self.navigationController pushViewController:sub animated:YES];
}
SubCatsViewController.h
@protocol SubCatsDelegate <NSObject>
- (void)didSelectSubCats:(SubCats *)cats;
SubCatsViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.cat.catId =
[[self.subCats objectAtIndex:indexPath.row] objectForKey:@"id"];
self.cat.name =
[[self.subCats objectAtIndex:indexPath.row] objectForKey:@"name"];
if ([self.delegate respondsToSelector:@selector(didSelectSubCats:)]) {
[self.delegate didSelectSubCats:self.cat];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end