Search code examples
iphoneios5pushviewcontrollerpopviewcontroller

Issue with popViewController and pushController


I'm facing issue with redirecting the viewControllers using pop/pushViewControllers. I've three view controller like below image -

Control Flow

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?


Solution

  • 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.
    
    }