在RootViewController
我有一個按鈕,當它被點擊時使self.navigationController
推到CategoryViewController
。使用委託傳遞值不起作用
然後,在CategoryViewController
中,單擊一個單元格推送到SubCatsViewController。當我選擇了一個單元格時,它應該將CategoryViewController
和SubCatsViewController
解散回RootViewController
。
但如何做到這一點?
如果我使用dismissViewControllerAnimated,它只會取消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
這不是根視圖控制器 – 2013-04-10 11:28:32