0

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
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
yong ho
  • 3,892
  • 9
  • 40
  • 81

1 Answers1

0

The documentation of dismissViewControllerAnimated:completion: in UIViewController (from which UINavigationController inherits) says that:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

That explains why SubCatsViewController (and only that view controller) gets dismissed. If RootViewController is indeed your root view controller you can change the line in your tableView:didSelectRowAtIndexPath: from

[self dismissViewControllerAnimated:YES completion:nil];

to

[self.navigationController popToRootViewControllerAnimated:YES];
andreag
  • 881
  • 8
  • 12