I've got a game with a MasterViewController. It has a "Play" button that segues to the GameViewController. The GameViewController and GameScene are plain vanilla what a game build provides except I added an NSLog to the GameScene's update method and, on the storyboard, created a "Quit" button that segues back from the GameViewController to the MasterViewController.
Everything works as expected. I fire up the app and touch the Play button and it transitions to the GameViewController to the GameScene. Fine, I see the standard "Hello World" message and can touch to create spinning spaceships. I start getting the NSLog output from the update method. Great.
However, when I click the Quit button and it segues back to the MasterViewController, I am still getting the NSLog output from the GameScene update method so the GameScene is still active. I want the GameScene gone. Added a dealloc to the GameScene and it is never called, probably because of ARC.
In the GameViewController I added a weak gameScene property and:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(#"viewWillDisappear");
[_gameScene removeAllChildren];
[_gameScene removeAllActions];
[_gameScene removeFromParent];
_gameScene = nil;
}
Still getting the NSLog output from the GameScene update method. Sigh... How do I kill the GameScene dead, dead, dead?
I did the Play/Quit/Play/Quit transition several times. The output from the update method is:
2014-11-20 12:48:41.551 Demo[7386:2004098] update: 0x7b091090
2014-11-20 12:48:42.095 Demo[7386:2004098] update: 0x7ed21020
2014-11-20 12:48:42.656 Demo[7386:2004098] update: 0x7eb1c4b0
2014-11-20 12:48:43.217 Demo[7386:2004098] update: 0x7b091090
2014-11-20 12:48:43.762 Demo[7386:2004098] update: 0x7ed21020
2014-11-20 12:48:44.322 Demo[7386:2004098] update: 0x7eb1c4b0
So all of my GameScenes are still active in the background.
You must make sure no other objects are pointing strongly to the object you want to remove from memory. See Apple Developer.
I found a workaround - use a nav controller, which I alway hide to make room for my game. To transition to the next view controller in the hierarchy, use a "Show" segue. To pop back, add your own Back button and connect the action:
- (IBAction)backButtonClicked:(UIButton *)sender {
[[self navigationController] popViewControllerAnimated:YES];
}
I also have one place in my GameViewController where I use a "Pause" button to pop all the way back to the root view controller.
- (IBAction)pauseButtonClicked:(UIButton *)sender {
NSArray * viewControllers = self.navigationController.viewControllers;
NSLog(#"nav view controllers: %#", viewControllers);
UIViewController * targetViewController = viewControllers[0];
NSLog(#"target controller: %#", targetViewController);
[self.navigationController popToViewController:targetViewController animated:YES];
}
All this correctly deallocates the GameViewController and GameScene when they disappear.
Related
I'm new to game development with SpriteKit and everything went ok so far, but cant really the memory management part.
I created a first UIViewController to act as a level selector, a second UIViewController in which a present the SKScene of the game. The problem appears when i go back from the SKScene to the level selector and none of the memory is released.
From the game SKScene, when the user pushes the back button to go to the level selector i post a notification which tells the second UIViewController to perform the segue.
//SKScene
[self removeAllActions];
[self removeAllChildren];
[self removeFromParent];
[[NSNotificationCenter defaultCenter]postNotificationName:#"toLevelSelector" object:nil userInfo:nil];
//Second UIViewController
- (void)toLevel:(NSNotification *)notif
{
[self performSegueWithIdentifier:#"toLevelSelector" sender:self];
}
Can you help me get a better understanding on when and where should be the skview or the skscene released from memory ?
I think the memory is not released because you did not dismiss view controller that holds SpriteKit. When I first tried to make game that has menus in UIViewControllers and then button starts SpriteKit game, I experienced that closing game does not end timers, sounds, music, etc...
Let's say you have UIViewController called MainMenuViewController and that it has a button that calls GameViewController in which you run SpriteKit Game. So in order to close your game completely, try to implement this method in your exit game button
[(GameViewcontroller *)self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
I'm implementing my own 'back' button. Where onClick, the following code is executed in the ViewController (VC) being dismissed:
Dismiss current VC (VC#1)
Pop current VC (VC#1) off my custom navigationStack
Get the last VC (VC#2) from the navigationStack, and present it using
presentViewController
What happens is the back works visually works - i.e. current VC disappears, previous VC appears. However, the viewDidLoad method is not called. So the screen isn't updated with data updates from viewDidLoad.
[self dismissCurrentViewController:self completion:^{
[TWStatus dismiss];
FHBaseViewController *vcToDisplay = [[FHDataManager sharedInstance] popNavigationStack];
[vcToDisplay.homeVC presentViewController:vcToDisplay animated:NO completion: ^{ }];
}];
Questions:
I was under the impression that viewDidLoad always gets called when presentViuewController is used??
I 'build' the screen using a method called ONLY from viewDidLoad in VC#2. How is iOS displaying the screen without coming into viewDidLoad?
btw, I'm not using storyboards. Any help is appreciated!
My guess is that viewWillAppear is being called but viewDidLoad is not, at least not when you expect it is. viewDidLoad should be called once, but depending on how you're managing the view controllers, viewDidLoad may not be triggered every time your view appears (which happens after loading).
The completion handler is called after the viewDidAppear: method is called on the presented view controller. from presentViewController doc
so put this in your code with a breakpoint on the call to super and verify it is getting called when this transition occurs.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
edit: since you verified that viewWillAppear is getting called, then I would say that it's coming down to how you are managing the view controller life cycle. Even with a standard UINavigationController, viewDidLoad is not called when a view is shown as a result of popping items on the navigation stack. I would move your logic to viewWillAppear if you are dead set on not using UINavigationController
When I make a back button pragmatically I use:
[self.navigationController popViewControllerAnimated:YES];
This will invoke the viewDidLoad method. Use that instead of your current code.
As part of my updating my apps to replace the deprecated presentModalViewController with presentViewController, I did some testing.
What I found was disturbing. Whereas presentModalViewController always works and there is no question about it working, I have found the presentViewController method often will not display my VC at all. There is no animation and it never shows up.
My loadView are called without problems, but the actual view does not appear.
So here is what I am doing:
User taps a button in my main view controller.
In the callback for that tap, I create a new view controller and display it as shown above.
The VC never appears (it is an intermittent problem though) but because this VC begins playing some audio, I know that its loadView was called, which looks like as follows.
My button-pressed callback is as follows:
- (void) buttonTapped: (id) sender {
VC *vc = [[VC alloc] init];
[self presentViewController: vc animated:YES completion: nil];
[vc release];
}
Here is my loadview in the VC class:
- (void) loadView {
UIView *v = [UIView new];
self.view = v;
[v release];
... create and addsubview various buttons etc here ...
}
Thanks.
Make sure the controller that calls the function has its view currently displayed (or is a parent to the one currently displayed) and it should work.
I am working with Automatic Reference Counting.
I have a custom UIViewController subclass and whenever I call -presentViewController: animated:completion: or remove its view from the superview I would like to NSLog something like "I am dealloced" so I know that the view controller has successfully been removed. I have implemented the -dealloc method in my view controller. However I started a test project where I just had two UIViewController instances (no retain cycles) and -dealloc is not called either when I push the second UIViewController modally or when I remove the superview or when I remove it from the parent view controller. Am I missing something ? In my original project (not the test case) Instruments shows me that those controllers leave a memory footprint that I can't get rid off.
If you want to switch view controllers, and have the one you're switching away from be deallocated, then just switch the root view controller of the window. So, if you're in VC1 and want to go to VC2, then do this in VC1:
VC2 *vc2 = [[VC2 alloc] init]; // or however else is appropriate to get an instance of this class
self.view.window.rootViewController = vc2;
If you haven't created any property to point to vc1, then it will be deallocated after making this switch.
If you want to use a modal presentation or a modal segue (to get the animation when you switch controllers), you can still get the initial controller to be deallocated by switching the root view controller after the presentation from the viewDidAppear method of vc2:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.view.window.rootViewController = self;
}
To get a print when the View Controller is deallocated you can implement the dealloc method as
- (void) dealloc {
NSLog(#"The instance of MyViewController was deallocated");
}
Then to get a print when the View Controller left the view you can implement
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(#"The instance of MyViewController left the main view")
}
If you use -presentViewController:animated:completion: you are retaining the parentViewController every time you call this method. ModalViewControllers are simply pushed on top of the other ViewController.
ModalViewControllers are only designed for some kind of information / User Input and stuff like that. If you want to dealloc the ParentViewController you have to deal with your own implementation.
dealloc method isn't called when the class is retained (or something in this class is retained) and not reeleased. It is justly for projects with both ARC and without it. So check your code twice.
I am creating an iPhone client for one of my apps that has an API. I am using the GTMOAuth2 library for authentication. The library takes care of opening a web view for me with the correct url. However I have to push the view controller myself. Let me show you some code to make things more clear:
- (void)signInWithCatapult
{
[self signOut];
GTMOAuth2ViewControllerTouch *viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:[_account catapultAuthenticaiton]
authorizationURL:[NSURL URLWithString:kCatapultAuthURL]
keychainItemName:kCatapultKeychainItemName
delegate:self
finishedSelector:#selector(viewController:finishedWithAuth:error:)];
[self.navigationController pushViewController:viewController animated:YES];
}
I have a "plus"/"add" button that I add to the view dynamically and that points to that method:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(signInWithCatapult)];
When I press the "add" button, what is supposed to happen is to open the web view with an animation, and then add an account to the accounts instance variable which populates the table view. This works fine if I add one account, but as soon as I try to add a second account, the screen goes black and two errors appear in the console:
nested pop animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
The only way that I found to avoid this problem was to disable animations when pushing the view controller.
What am I doing wrong please?
Typical situations
You push or pop controllers inside viewWillAppear: or similar methods.
You override viewWillAppear: (or similar methods) but you are not calling [super viewWillAppear:].
You are starting two animations at the same time, e.g. running an animated pop and then immediately running an animated push. The animations then collide. In this case, using [UINavigationController setViewControllers:animated:] must be used.
Have you tried the following for dismissing once you're in?
[self dismissViewControllerAnimated:YES completion:nil];
I got the nested pop animation can result in corrupted navigation bar message when I was trying to pop a view controller before it had appeared. Override viewDidAppear to set a flag in your UIViewController subclass indicating that the view has appeared (remember to call [super viewDidAppear] as well). Test that flag before you pop the controller. If the view hasn't appeared yet, you may want to set another flag indicating that you need to immediately pop the view controller, from within viewDidAppear, as soon as it has appeared. Like so:
#interface MyViewController : UIViewController {
bool didAppear, needToPop;
}
...and in the #implementation...
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
didAppear = YES;
if (needToPop)
[self.navigationController popViewControllerAnimated:YES];
}
- (void)myCrucialBackgroundTask {
// this task was presumably initiated when view was created or loaded....
...
if (myTaskFailed) { // o noes!
if (didAppear)
[self.navigationController popViewControllerAnimated:YES];
else
needToPop = YES;
}
}
The duplicated popViewControllerAnimated call is a bit ugly, but the only way I could get this to work in my currently-tired state.