在应用程序生命周期中何处删除委托?(Where to remove delegate in app life cycle?)

编程入门 行业动态 更新时间:2024-10-26 11:27:11
在应用程序生命周期中何处删除委托?(Where to remove delegate in app life cycle?)

我有一个类的实例,它的委托只能调用一次。

一个视图控制器(视图A)将调用self.delegate = self ,并有按钮去其他视图控制器(VC)。

在其中一个VC中,我需要调用此实例并再次设置委托。

最初我在viewWillAppear设置了委托,但这不起作用,因为我在viewDidLoad加载委托数据,并且由于viewDidLoad会先运行,我可以在viewDidLoad或loadView设置委托。

而删除委托可以在dealloc ,或viewWillDisappear ,但如果我提出一个VC,视图A的dealloc不会被调用是不是? 由于该视图尚未删除。

所以我的问题是,我在哪里设置self.delegate = nil在视图A?

如果我使用现在的VC方法,我应该在哪里设置和删除视图A中的委托? 在呈现的VC中,我可以通过viewDidLoad和dealloc设置和删除。

根据应用生命周期的最佳做法提出此问题。

我查看了iOS uiviewcontroller的生命周期

I've an instance of a class, which its delegate can only called once.

A view controller (View A) will call self.delegate = self, and has buttons to go to other view controller (VC).

In one of the VC, I need to call this instance and set the delegate again.

Initially I set the delegate in viewWillAppear but that won't work, because I'm loading the delegate data in viewDidLoad, and since viewDidLoad will run first, I can either set delegate in viewDidLoad or loadView.

Whereas removing delegate can be in dealloc, or viewWillDisappear, but if I'm presenting a VC, dealloc of View A won't be called isn't it? As the view is not removed yet.

So my question is, where to I set self.delegate = nil in view A?

If I'm using present VC method, where should I set and remove the delegate in View A? While in presented VC, I can just set and remove via viewDidLoad and dealloc.

Asking this in terms of best practice for app life cycle.

I've viewed iOS uiviewcontroller life cycle

最满意答案

在问之前我应该​​自己测试一下。

无论如何,对于其他可能有兴趣知道的人也是如此。

查看A将去dealloc去其他viewController(视图B)。 因此,可以安全地在View A的dealloc中删除委托,并在View B中设置委托。

如果View B通过在View A中单击来呈现,则不会调用dealloc。 所以在这种情况下,最好分别使用viewWillAppear和viewWillDisappear来设置和删除委托。

干杯。

I should have test this myself before asking.

Anyway, for others who might be interested to know as well.

View A will dealloc when going to other viewController (View B). So it is safe to remove delegate at View A's dealloc, and set the delegate at View B.

Where as if View B is presented by clicking on a in View A, dealloc won't be called. So in this case, it'll be better to use viewWillAppear and viewWillDisappear for setting and removing delegate respectively.

Cheers.

更多推荐

本文发布于:2023-07-30 06:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生命周期   应用程序   remove   delegate   life

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!