Search code examples
ioscocoa-touch

Navigating to a main ViewController that is already loaded without loading it again


I have a main view controller that loads some data from API, from there I move to another screen which shows details for a particular deal. I want to go back to the main controller, but when I use prepareForSegue, it goes back to the main controller, but it loads the data again making an API call. I want to retain the state of the main controller.

Please help me as how to retain the state of a view controller and how to segue from the second controller to main controller without loading it again.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self FetchDeals];
    [self Getdeals];
}

The fetch deal method makes an API call and getdeals populates it into a tableview.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Dealdetail"])
    {
        //NSLog(@"New controller");
        DealDetailController *detail = (DealDetailController *)[segue destinationViewController];
        
        detail.detailimage = imagepath;
    }
}

DealDetailController is the second view controller from where I come back to the main control.

Code in DealDetailController:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    MainappViewController *maincontrol = (MainappViewController *)[segue destinationViewController]; 
}

Solution

  • What you really want to use here is an 'Unwind Segue'.

    This blog post describes (with videos) how to do this.

    Also refer to this question where the answer points you toward the WWDC 2012 video which covers Unwind segues.