Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController to know if it got pushed or popped?

I have a main UITableView, when cell is pressed it goes to another UITableView and when a cell is pressed there it goes to a DetailView of that cell.

I want the middle UITableView to behave differently depending on if the detailView got popped or the UITableView itself got pushed. If the view got pushed on from the main table I want to scroll to the top, if it is shown after a DetailView got popped I want it to stay at the same position.

Any suggestions?

like image 880
Nings Avatar asked Oct 24 '22 15:10

Nings


2 Answers

you could call a scrollToTop method on the DetailViewController after you have pushed it to the navigationController.

Something like that:

if (!detailViewController) {
    detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
}
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController scrollToTop];
// or use the tableView directly: 
// [detailViewController.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
like image 72
Matthias Bauch Avatar answered Oct 31 '22 12:10

Matthias Bauch


In your Middle View Controller, examine which view is next-to-display directly from the UINavigationController stack:

- (void)viewWillDisappear:(BOOL)animated
{
    if ([self.navigationController.topViewController isEqual:(UITableViewController *)tvcDetailView]) {
        // Detail view has been pushed onto the UINavigationController stack
    }
    else {
        // Middle view has been popped from the UINavigationController stack
    }
}
like image 42
paul.lander Avatar answered Oct 31 '22 11:10

paul.lander