Retain SourceViewController after custom push segue

别说谁变了你拦得住时间么 提交于 2019-12-12 02:02:50

问题


I've got a custom UIStoryboardSegue called leftToRightSegue. Every time this segue is triggered and I move back to the source view, the viewdidload method of the sourceViewController is called. This is not something I want, since the setup is quite heavy.

This is the code I'm using for the segue:

#import "LeftToRightPushSegue.h"
#import "QuartzCore/QuartzCore.h"

@implementation LeftToRightPushSegue

-(void)perform {

    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UIViewController *destinationController = (UIViewController*)[self destinationViewController];

    CATransition* transition = [CATransition animation];
    transition.duration = .25;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;

    [sourceViewController.navigationController.view.layer addAnimation:transition
                                                                forKey:kCATransition];

    [sourceViewController.navigationController pushViewController:destinationController animated:NO];

}

I am in fact popping the view back, for anyone interested:

UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];

CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;

[sourceViewController.navigationController.view.layer addAnimation:transition
                                                            forKey:kCATransition];

[sourceViewController.navigationController popViewControllerAnimated:NO];

How do I retain the sourceViewController?


回答1:


I would try to move the code to the init method if possible. If that's not possible, perhaps create a boolean property. i.e.

@property (nonatomic, strong) BOOL *isInitialised;



- (void) viewDidLoad
{
    if (!self.isInitialised) 
    {
       ...
       self.isInitialised = YES;
    }
}

You set it to true once the actions in the viewDidLoad have been performed. There are better patterns if you search a little. This is a bit of quick and dirty way of getting it to work.

Also keep in mind that you'll be keeping in memory stuff, not directly visible to the user.



来源:https://stackoverflow.com/questions/27857172/retain-sourceviewcontroller-after-custom-push-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!