自定义推segue后保留SourceViewController [英] Retain SourceViewController after custom push segue

查看:26
本文介绍了自定义推segue后保留SourceViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 leftToRightSegue 的自定义 UIStoryboardSegue.每次触发这个 segue 并且我移回源视图时,都会调用 sourceViewControllerviewdidload 方法.这不是我想要的,因为设置很重.

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];

如何保留 sourceViewController?

How do I retain the sourceViewController?

推荐答案

如果可能,我会尝试将代码移至 init 方法.如果这是不可能的,也许创建一个布尔属性.即

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;
    }
}

一旦执行完 viewDidLoad 中的操作,就将其设置为 true.如果您稍微搜索一下,就会有更好的模式.这是让它工作的一种快速而肮脏的方式.

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.

这篇关于自定义推segue后保留SourceViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆