Question

I am in need of some help please. I have been stuck on this issue all day and I cannot find a fix. I will try and be as clear as possible with my problem.

I have 2 ViewControllers. One is called ViewController and the other is called GameViewController. In the ViewController I have my home screen menu and on the GameViewController I have the Game screen AND the Game Over screen. When you press play from ViewController it takes you to the Game, and then when you die you stay in the same GameViewController but in a Game Over view. If you press restart in the Game Over screen a segue takes you back modally to the Game Screen.

So here is the problem I am having.

When you die for the first time and go to the Game Over screen, you can click on home and it will take you home, BUT if you press restart, then die again (that's twice ) and then try to press home from the Game Over Screen it will NOT run the code inside the home button. The button actually works, I have an NSLog in their and it shows in the debugger screen. Weird I know. Please see below code that will show everything I have done to try and make this work.

In GameViewController.h //Above all the #imports

@class GameViewController;

@protocol GameViewControllerDelegate <NSObject>

-(void) GameViewControllerDidCancel:(GameViewController *)controller;
- (IBAction)homeBtn:(id)sender;

@end

And this just after the the close } in @Interface

@property (nonatomic, weak) id <GameViewControllerDelegate> delegate;

In GameViewController.m after the @Implementation

@synthesize delegate = _delegate;

And this in my homeBtn

- (IBAction)homeBtn:(id)sender {

    NSLog(@"home btn pressed");

   [self.delegate GameViewControllerDidCancel:self];

}

Then in my ViewController.h

#import "GameViewController.h"

    @interface ViewController : UIViewController <GameViewControllerDelegate> 

ViewController.m

When I press play

- (IBAction)playButton:(id)sender {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main_iPad"
                                                  bundle:nil];
    GameViewController* controller= [sb instantiateViewControllerWithIdentifier:@"GML"];
    controller.delegate = self;
    [self presentViewController:controller animated:NO completion:nil];
    [audioPlayer stop];
    }else {

        UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main_iPhone"
                                                      bundle:nil];
        GameViewController* controller= [sb instantiateViewControllerWithIdentifier:@"GML"];
        controller.delegate = self;
        [self presentViewController:controller animated:NO completion:nil];
        [audioPlayer stop];
    }

}

And the method that I try to call from my homeBtn is also in here

#pragma mark - GameViewControllerDelegate

- (void) GameViewControllerDidCancel:(GameViewController *)controller {

    [self dismissViewControllerAnimated:NO completion:nil];
}

I am pretty sure that is it. So like I said before, the restart button is just linked to a segue to return back to the Game screen.

Please guys, I am really desperate now, I am completely out of ideas.

Thank you

Was it helpful?

Solution

Quite a convoluted view hierarchy and hard to understand.

What exactly does the restartGameButton do. Is it an unwind segue?

When you press play from ViewController it takes you to the Game, and then when you die you stay in the same GameViewController but in a Game Over view. If you press restart in the Game Over screen a segue takes you back modally to the Game Screen.

What is the GameOver view thing? Once you say its a view in the GameViewController, but then again you say if you press restart a segue takes you back modally to the Game Screen (i.e. GameViewController) which implies the GameOver Screen is also a ViewController.

Why use modal presentation when it is a actually a navigation stack that you want?

Best solution in my opinion:

Make 3 ViewControllers, one for the Start, one for the Game, one for GameOver. Embed the ViewControllers in a navigation controller and push and pop with this one instead of presenting many modal views on top of one another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top