在RootViewController
我有一个按钮,当它被点击时,它会self.navigationController
推动到CategoryViewController
.
然后,在 中CategoryViewController
,单击一个单元格推送到 SubCatsViewController。当我选择了一个单元格时,它应该解散CategoryViewController
并SubCatsViewController
返回RootViewController
.
但是怎么做呢?
如果我使用dismissViewControllerAnimated,它只会dismiss,而SubCatsViewController
不是CategoryViewController
。我已经写了一个委托,SubCatsViewController
所以我可以从中获取选定的值SubCatsViewController
,并且RootViewController
我符合这个委托协议,以获得我想要的值。
但是,我无法使用我编写的委托获取值。
- (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