1

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 2nd 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]; 
}
Prasad Devadiga
  • 2,573
  • 20
  • 43
ankit_rck
  • 1,796
  • 2
  • 14
  • 24

2 Answers2

1

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.

Community
  • 1
  • 1
Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
  • 1
    Sounds good. Is this limited to ios6? Not sure if that's of interest to the OP but just wanted to clarify. Also, I'm interested in the context of this [SO question - viewdidload in ios 6 called once](http://stackoverflow.com/questions/13152304/viewdidload-in-ios-6-called-once). This may be useful to the OP. – wmorrison365 May 22 '13 at 13:02
  • Yes, good point. I think they are available only from iOS6. Having said that it's the same for Segues in general which are already in use by the OP. – Mike Pollard May 22 '13 at 13:06
0

use popViewController. it retains the state. already you have loaded mainViewController that means it is on the stack.if you want again this viewController then simply use popViewController

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } It still Loads in main controller – ankit_rck May 22 '13 at 08:19
  • @ankit_rck - you need to read up on Unwind Segues as per my answer. – Mike Pollard May 22 '13 at 12:48