Search code examples
iosobjective-cuiviewcontrollerstatepersist

How to persist my View Controller


When I leave my View Contoller, I use:

[self dismissViewControllerAnimated:YES completion:nil];

Does this 'destroy' the View Controller and it's state?

I want to leave the View Controller in the state it's in, scrollViews with buttons on etc and then return to it when I press the parent's button that handles entry to the VC. I use code in the parent like this:

- (void)handleDoubleTap:(UITapGestureRecognizer *)doubleTap {

realTaggingScreen = [[TaggingScreenViewController alloc]init];
realTaggingScreen.topLeftCornerImage = self.imageToPresent;
realTaggingScreen.salescallDate = self.salescallDate;
realTaggingScreen.shopName = self.shopName;
realTaggingScreen.refToThumbnailsVC = self.presentingThumbnailViewController;
realTaggingScreen.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:realTaggingScreen animated:YES];

}

I know this is OBVIOUSLY wrong due to alloc init.

I tried to give my Tagging Screen a reference to the parent and a BOOL called iExist as well as giving my parent a reference to the Tagging Screen, then if the BOOL is true, open the reference to the child, otherwise alloc init etc but this didn't work. I can't help thinking this isn't the right way to go about it....

I will also want to save certain objects in the NSDocuments Directory and load them etc between app runs but I know how to do that. For now I'd settle for a way to make the VC state temporarily persist

Any help would be good.


Solution

  • if you are using ARC, the view controller will be released if no pointers are pointing to it anymore.

    I would reccommend creating a strong pointer to an instance of realTaggingScreen

    something like

    @property(nonatomic, strong)TaggingScreenViewController *realTaggingScreen;
    

    if i get what you are trying to do, you dont need a BOOL iExist. you can use

    self.realTaggingScreen = nil; 
    

    if you don't need it to persist anymore then you can check if you need to allocate a new instance using

     if(self.realTaggingScreen) {
    //initialize...
    }