I have a UITableViewController that when a cell is pressed, I want the controller to pop itself, and then have the controller it pop\'s to, push another view controller onto the
I liked Alexandre's solution but if I were a delegate person, I wouldn't want to use notification. So in that case, we can just use the delegate in the viewDidDisappear
method.
So, in the didSelectRowAtIndexPath
method, you can pop the controller-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.navigationController popViewControllerAnimated:YES];
}
and in the same View Controller, the ViewController you are popping, call the delegate method in the viewDidDisappear
method like-
-(void)viewDidDisappear:(BOOL)animated{
[self.delegate cellPressedInTableViewControllerWithCalculationsModel:(id)anArgmentMyDelegateMethodTakes];
}
Then in the controller that is pushed can implement the delegate method and inside that delegate method, you can do whatever you want.
Don't pop it from the top level controller. Call the delegate method that will pop the view off the navigation controller and then push a new one on.
You should use a flag to overcome this situation. You set this flag in viewWillDisappear method of view controller being popped. When this flag is set then and then you can push another view controller on stack. Hope it's clear.
How about when you dismiss your UIViewController containing the table you send a NSNotifcation in your viewDidDisappear method like so:
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"loadOtherVC" object:nil];
}
And in your parent view controller that will push a new view controller, you add an observer for that notification like so:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoadOtherVC:) name:@"loadOtherVC" object:nil];
You should have a method that matches the selector.
- (void) LoadOtherVC:(NSNotification *) notification
{
// load your other view controller you want here
}
The cleanest way to do more than a single push or pop of a view controller is to set the UINavigationController
s view controllers array.
For example, to pop and then push a view controller:
MyTableViewController *vc = [[MyTableViewController alloc] init];
NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[controllers removeLastObject];
[controllers addObject:vc];
[self.navigationController setViewControllers:controllers animated:YES];