调用 popToViewController 的正确方法 - iOS [英] Proper way to call popToViewController - iOS

查看:34
本文介绍了调用 popToViewController 的正确方法 - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与我的团队成员就 popToViewController 发生争执.

I am having an argument with my team member regarding popToViewController.

对我来说最好的方法是我可以弹出到特定的 ViewController 吗?

但他认为我们应该创建一个viewController的静态实例然后调用

But he think we should create a static instance of the viewController and then call

-(void)takeToPreviousViewController {
    if([IVPreviousViewController sharedInstance]) {
        [self.navigationController popToViewController:[IVPreviousViewController sharedInstance] animated:YES];
    }
}

我强烈反对这种方法,因为我们在 IVPreviousViewController 中创建了一个与 currentViewController 无关的公共方法.我们应该始终避免这样的方法暴露.

I strongly appose this approach because we're creating a public method in IVPreviousViewController which has no relation to the currentViewController. We should always avoid method expose like this.

谁能给我一个关于最佳方法的可靠理由.

Can anyone point me a solid reason about the best approach.

更新:

IVPreviousViewController.h

IVPreviousViewController.h

__weak static IVPreviousViewController * staticEventDetailViewController;

    +(IVPreviousViewController *)sharedInstance;

IVPreviousViewController.m

IVPreviousViewController.m

+(IVPreviousViewController *)sharedInstance {
    return staticEventDetailViewController;
}

这种方法的原因 - 在我们的 viewController 中的一个特定用例中,它必须 popbackto IVPreviousViewController

Reason of this approach - On a particular use case in our viewController it has to popbackto IVPreviousViewController

推荐答案

这是我的看法:

我支持你的观点.我们应该尽可能避免静态共享实例.静态实例保留在内存中,直到应用程序生命周期为止.从此实例引用的所有其他对象也将保留在内存中.

I support your opinion. We should avoid static shared instances where ever possible. Static instances remains in memory until app life time. All the other objects being referenced from this instance also then remain in memory.

使用上述共享链接 VC 中提到的方法,不再需要的将从内存中取出.此外,如果您没有很多 VC,最好从源 VC 对目标 VC 进行弱引用,而不是循环并选择目标 VC.

With the approach mentioned in above shared link VC that is no more needed will be taken out of memory. Also, if you do not have many VCs, better have a weak reference to target VC from source VC instead of looping and selecting the target VC.

是的,在目标 C 的上下文中,为了更好的可读性,头文件应该只包含外部世界真正需要的那些 API.

And yes, in context of objective C for better readability, header file should contain only those APIs that are really needed by outside world.

这篇关于调用 popToViewController 的正确方法 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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