user5127227
user5127227

Reputation:

Attempt to present whose view is not in window hierarchy ( when using segue ) objective - c

I have 2 View Controllers and I get the error above using this:

-(void)hitwtf{

    if (CGRectIntersectsRect(ufo.frame, sanchez.frame)) {
        if(((sanchez.frame.origin.y + sanchez.frame.size.height - 5) <= (ufo.frame.origin.y)) && (sanchezJumping < 0))
        {

            [self performSegueWithIdentifier:@"pushToGameOver" sender:self];
}        
}
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if([segue.identifier isEqualToString:@"pushToGameOver"]){

    AudioServicesPlaySystemSound(sanchezdeath);


    GameOverViewController *controller = (GameOverViewController *)segue.destinationViewController;
    controller.CurrentScore = CurrentScore;

        }
}

It works, the view gets displayed but I am just getting this error. I don't use any buttons, the view should change when two frames hit each other, so I need the performSegueWithIdentifier, don't I?

EDIT:

Answer is :

- (void)viewWillDisappear:(BOOL)animated {
   if(timer)
   {
     [timer invalidate];
     timer = nil;
   }
 }

Upvotes: 0

Views: 39

Answers (1)

mrtna
mrtna

Reputation: 191

I have two ideas for your problem (in response to the comments.):

Either move the sanchez / ufo back to the place it was before the collision.

Or add a boolean

@property (nonatomic, assign) BOOL collision;

And add this in your method:

if(!self.collision && ((sanchez.frame.origin.y + sanchez.frame.size.height - 5) <= (ufo.frame.origin.y)) && (sanchezJumping < 0))
    {
        self.collision = YES;
        [self performSegueWithIdentifier:@"pushToGameOver" sender:self];

Upvotes: 1

Related Questions