a Custom Segue that Simulates a Push Segue turns VC into Zombie

后端 未结 3 2037
粉色の甜心
粉色の甜心 2021-02-10 19:17

[To Make things short and clear]

I\'ve written a custom segue.

-(void)perform {
UIView *preV = ((UIViewController *)self.sourceViewController).view;
UI         


        
3条回答
  •  不知归路
    2021-02-10 19:39

    You're getting a crash just because your new controller not retained after segue execution.

    What you do is this:

    • src and dest controllers are instantiated
    • you perform your animations
    • in completion you remove src view
    • your src controller gets released, but window's rootViewController still pointing to it and your destination view controller not added to window's hierarchy.

    This will work just as intended:

    -(void)perform {
      UIView *preV = ((UIViewController *)self.sourceViewController).view;
      UIView *newV = ((UIViewController *)self.destinationViewController).view;
    
      UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
      newV.center = CGPointMake(preV.center.x + preV.frame.size.width, newV.center.y);
      [window insertSubview:newV aboveSubview:preV];
    
      [UIView animateWithDuration:0.4
                       animations:^{
                           newV.center = CGPointMake(preV.center.x, newV.center.y);
                           preV.center = CGPointMake(0- preV.center.x, newV.center.y);}
                       completion:^(BOOL finished){
                           [preV removeFromSuperview];
                           window.rootViewController = self.destinationViewController;
                       }];
    }
    

提交回复
热议问题