问题
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