Reputation: 24476
I'm facing issue with redirecting the viewControllers using pop/pushViewControllers. I've three view controller like below image -
In my 3rd view i've some ToggleButton
based on that toggleButton
the data which is in 2nd View will change. After, changing something on 3rd view and press Done
button in my 3rd View i just started my 2nd ViewController using pushViewController
And, the changes are occured successfully.
But, what was the issue is, when i try to press the back button on my 2nd view page its redirecting me again to 3rd viewController (Because of pushViewController)
I try to start my 2nd view using popViewController
but, its giving exception.
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.
I've used below code -
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
second.array = resultArray;
second.indexValue = ind;
[self.navigationController popToViewController:second animated:YES];
How do i solve this issue? Any idea?
Upvotes: 0
Views: 1626
Reputation: 2163
If you want to go to your current previous view controller then you have to do this:
[self.navigationController popViewControllerAnimated:YES];
Upvotes: 1
Reputation: 1885
From 3rd viewcontroller you just need to call this method to pop to 2nd viewcontroller :
[self.navigationController popViewControllerAnimated:YES];
EDIT :
For notifying the second vc for data change you can use delegate. Follow this :
On ThirdViewController.h
@protocol DataChangeProtocol;
@interface ThirdViewController : UIViewController
@property(nonatomic,assign) id<DataChangeProtocol> delegate;
@end
@protocol DataChangeProtocol
- (void)thirdViewcontroller:(ThirdViewController*)vc dataChangedto:(NSDictionary *)changedData;
@end
And before pushing the thridVC from secondVC assign secondVC as the delegate of thirdVC :
ThirdViewController *thirdVC = [[ThirdViewController alloc]init..];
thirdVC.delegate = self;
[self.navigationController pushViewController:thirdVC animated:YES];
and in SecondViewController.m
- (void)thirdViewcontroller:(ThirdViewController*)vc dataChangedto:(NSDictionary *)changedData {
// Change your values in the UI using the passed value from changedData.
}
Upvotes: 1
Reputation:
//it used in first class
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
second.array = resultArray;
second.indexValue = ind;
[self.navigationController pushViewController:second animated:YES];
//it move to second class
//it used in second class
[self.navigationController popViewControllerAnimated:YES];
//it move to firsr class
Upvotes: 1
Reputation: 4140
You create our viewcontroller:
SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
and then you want to pop it from your stack but this controller is not in the stack, bacause you create it in the third viewController.
Upvotes: 1