UIView transition causing crash

2019-08-19 04:36发布

问题:

- (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.

回答1:

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 some crash log here in your question). But i'll suggest you no need to present secondPage Again .Here is your Code i made Single Line Change here .

   - (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:

You should comment below sinlge because you have already added secondPage as a Subview on currentView.so further no need to present it again.

                   //  [self presentViewController: secondPage animated:NO completion:NULL];
                     //This will make the next page load correctly after the transition, otherwise you cannot. interact with anything.
                 }];

}

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 this

   if(checkIfDeviceIsInlandscae)
    {
       secondPage.view.frame = CGRectMake(568, 0, 568, 320);

    } 

   else 
    {
      secondPage.view.frame = CGRectMake(320, 0, 320, 568);
    }

Inside the animation block change the frame according to the orientation, as i did above tow line.



回答2:

One problem is by calling both presentViewController: and addSubview: you are adding the view twice. I suspect you find it necessary to call presentViewController: because it invokes viewDidAppear: and you're using that to hook up the UI in secondPage. I'd put a logging statement in MainGameDisplay's viewDidAppear: 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].



回答3:

Your problem is one of memory allocation/deallocation. You're not retaining secondPage anywhere, and as such it will be deallocated after the StartGame method logic is completed. This deallocation will in turn deallocate MainGameDisplay's ivars/properties too, including MainGameDisplay.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 an EXC_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:

@interface ParentClass ()
{
    MainGameDisplay *_secondPage;
}

@end

...

- (IBAction)StartGame:(id)sender
{
    _secondPage = [[MainGameDisplay alloc] initWithNibName:nil bundle:nil];
    [self.view addSubview:_secondPage.view];

    ...

}

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.