- (IBAction)StartGame:(id)sender
{
MainGameDisplay *secondPage = [[MainGameDisplay alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:secondPage.view];
secondPage.view.frame = CGRectMake(568, 0, 568, 320);//(N = horizontal, N = vertical)
[UIView animateWithDuration:1.0f
animations:^{
//actual frame needed with an animation.
secondPage.view.frame = CGRectMake(0, 0, 568, 320);
}
completion:^(BOOL finished) {
//ENTER HERE ANYTHING TO RUN AFTER ANIMATION IS COMPLETED:
[self presentViewController: secondPage animated:NO completion:NULL];
//This will make the next page load correctly after the transition, otherwise you cannot. interact with anything.
}];
}
This works perfectly for entering the view. Without [self presentViewController: secondPage animated:NO completion:NULL];
, you cannot interact with the new page, any button you click causes it to crash.
If I return back to the first page, doing it normally, it's fine, but if I click StartGame again, the program changes views and then simply crashes. The problem is with the line [self.view addSubview:secondPage.view];
, if I comment this out, the program doesn't crash but when it changes views it doesn't transition and instantly appears.
The transition is also pretty laggy/glitchy in landscape mode, perfect in portrait though.
One problem is by calling both
presentViewController:
andaddSubview:
you are adding the view twice. I suspect you find it necessary to callpresentViewController:
because it invokesviewDidAppear:
and you're using that to hook up the UI in secondPage. I'd put a logging statement in MainGameDisplay'sviewDidAppear:
method and see when it is getting called.One fix may be to just replace
[self presentViewController: secondPage animated:NO completion:NULL]
with[secondPage viewDidAppear:NO]
.Here you are Adding
secondPage
on currentView as aSubview. and Again your presenting it. I think it causing some memory related thing may be some other thing(it would be much clear if you put somecrash log
here in your question). But i'll suggest you no need to presentsecondPage
Again .Here is your Code i made Single Line Change here .You should comment below sinlge because you have already added secondPage as a Subview on currentView.so further no need to present it again.
}
Edit: For Landscape Mode you should change coordinate while you are setting the frame
secondPage
view . i think here you are creating this view for 4 inch Devices. here you can do this by setting some checks as thisInside the
animation
block change the frame according to the orientation, as i did above tow line.Your problem is one of memory allocation/deallocation. You're not retaining
secondPage
anywhere, and as such it will be deallocated after theStartGame
method logic is completed. This deallocation will in turn deallocateMainGameDisplay
's ivars/properties too, includingMainGameDisplay.view
. This will all happen before you start to interact with the displayed view, so the moment you try to change values or interact with the no-longer-existent view, you're going to crash with anEXC_BAD_ACCESS
error.If you truly want to manage
MainGameDisplay.view
outside of the view controller, you'll need to retain secondPage in an ivar or member variable in your parent class:ParentClass.m:
However, this is not generally a recommended design pattern. It's the responsibility of a view controller to manage its view, particularly layout changes that might be made, animations in how it's displayed, etc. You're violating the encapsulation responsibilities of
MainGameDisplay
. You should consider doing whatever view management you need to do inside of MainGameDisplay.I would recommend reading through the View Controller Programming Guide for iOS. It will explain the fundamentals around what you're trying to do, and best practices for doing it.