0

I need to design a game by levels in which it is only possible, from each level, going forward or back to the home. it is not necessary to go back to the previous level. That's the scenario:

MENU->LEVEL 1 -> LEVEL 2 -> ...... LEVEL 9 -> LEVEL 10

So I extended the UIViewController Class and I assigned it to 10 objects in the storyboard. When certain conditions (it doesn't matter which ones) occur, I need to pass to the following level, discarding the memory of the current level, which is not necessary anymore. What's the best way of doing it? Please answer asap and thanks

2 Answers2

1

If you want to be able to sequentially move forward from VC1(home)-->VC2-->VC3 but always jump backwards to VC1(home) then use an unwind segue. This lets you do exactly what you want and provides further ability to jump back to any prior VC. And by the way, the unwind handles the memory issue for you. It's really easy to implement and I suggest having a look at this excellent post:

What are Unwind Segues for and How to Use Them

Community
  • 1
  • 1
Wizkid
  • 1,015
  • 10
  • 10
  • I can use the unwind segue to jump back to the previous level. Instead, I need to clear the memory occupied by the current viewcontroller and to pass through the segue to a new viewcontroller (which, from what I understand, would be the son of the viewcontroller I'm dismissing!) If, for example, I'm at level 9, without handling the memory, levels 1-2-3-4-5-6-7-8-9 would be loaded on the memory. That's a waste of memory, considering that the levels from 1 to 8 are not needed anymore. – snakecharmer Jul 15 '14 at 11:05
  • ARC takes care of that for you (assuming you're not on a pre-4.2 version of ios). Once the VC is popped the objects allocated and the memory reserved by those VC's reference count is dropped allowing the memory to be reclaimed. This is why you don't/can't invoke dealloc, or implement or invoke retain, release, retainCount, or autorelease with ARC. Check out https://developer.apple.com/library/ios/releasenotes/objectivec/rn-transitioningtoarc/introduction/introduction.html – Wizkid Jul 15 '14 at 13:57
0

shouldPerformSequeWithIdentifier is called before prepareSegue. So you can do your logic here to prevent the actual segue. Just return false and do what you need.

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if ([identifier isEqualToString:@"mySegue"]) {
        // Do what you need here
        [self.navigationController popToRootViewControllerAnimated:YES];
        return FALSE;
    }

    return TRUE;
}
Automate This
  • 30,726
  • 11
  • 60
  • 82
  • What instructions should I use to delete the sourcing viewcontroller and to pass to the destination viewcontroller? Please anwer asap and thanks – snakecharmer Jul 15 '14 at 11:07
  • I think your looking for `popToRootViewControllerAnimated` - see my edit above. – Automate This Jul 15 '14 at 13:43
  • I created a ViewControllers chain controlled by a NavigationViewControllorer connected by personalized segues. Each ViewController, once it's finished, programmatically executes the personalized segue: (void)perform{ UINavigationController *nav = [[self sourceViewController] navigationController]; UIViewController *destController = self.destinationViewController; [nav popViewControllerAnimated:NO]; [nav pushViewController:destController animated:YES]; }. When it's tapped the home button is called popToRootViewControllerAnimated. Is the memory cleaned up in both cases? – snakecharmer Jul 15 '14 at 22:18
  • I believe so yes, but I'd recommend using xCode Instruments to watch your memory and make sure. [Here is a link](http://www.raywenderlich.com/23037/how-to-use-instruments-in-xcode) that might help if your not familiar with instruments. – Automate This Jul 15 '14 at 22:31
  • Can the personalized segue that is used to connect to levels have the same identifier, considering that I must repeat the same operation every time? – snakecharmer Jul 15 '14 at 23:03