0
votes

I've built an app that populates a tableview with numerous items. You can click on an item and it will open a detailed view of that item. You can then click a button that opens another view that shows specifics for the detailed item. I also have a few more nested views that are shown until you get to a completion view. I want a button click to pop me straight back to the tableview but haven't found a way. I've tried the following ideas:

1st (which will only dismiss to the previous screen): [self dismissViewControllerAnimated:YES completion:nil];

2nd (doesn't work at all): [self.navigationController popToRootViewControllerAnimated:NO];

3rd (doesn't do anything): WorkOrderTableViewController *pushTable = [[WorkOrderTableViewController alloc] init]; [UINavigationController pushViewController:pushTable animated:YES];

4th (doesn't do anything): WorkOrderTableViewController *pushTable = [[WorkOrderTableViewController alloc] init]; [self.navigationController pushViewController:pushTable animated:YES];

I've even tried calling the segues from previous screens/views. Also tried a class within a previous view controller.

Is there a way to jump right back to the table view? If so, how?

1
What about popToViewController:animated:?Phillip Mills
I tried: WorkOrderTableViewController *pushTable = [[WorkOrderTableViewController alloc] init]; [self.navigationController popToViewController:pushTable animated:YES]; But it did nothing as well. Here is some more explanation as it maybe my lousy coding skills. Once in the detail view of the table item, you click a button that pushes to a service screen which has info to be put in. There is a continue button that pushes to the next screen where additional info can be put in. An additional button takes it to the final screen where I have the complete button. Help any?Jim

1 Answers

0
votes

Concerning the code in your above comment....

When you use alloc you're creating a new object. What you want is to go back to an already existing view controller. Save the original instance of WorkOrderTableViewController somewhere (app delegate? custom parent controller?) so that you can return to it.

For example, you could:

Create a subclass of UINavigationController that responds to a notification by popping to the correct view controller. (You need your own definition of what "correct" means.)

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToTable) name:kReturnToTable object:nil];
}

- (void)popToTable {
    UIViewController *target = nil;
    for (UIViewController *controller in [self viewControllers]) {
        if ([controller isKindOfClass:[UITableViewController class]]) {
            target = controller;
            break;
        }
    }
    if (target) {
        [self popToViewController:target animated:YES];
    } else {
        NSLog(@"Error -- couldn't find my controller in %@", [self viewControllers]);
    }
}

Make the final controller's button post that notification. (Notification name is defined in the last controller's header file.)

- (IBAction)backToTable:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:kReturnToTable object:nil];
}