保留 UIViewController 或不在导航堆栈中

发布于 2024-09-08 10:40:41 字数 418 浏览 1 评论 0原文

我有一个场景,我不确定是否保留视图控制器。假设我有 ViewControllerOne。当按下按钮时,ViewControllerOne 会将 ViewControllerTwo 推送到导航堆栈中。 ViewControllerTwo 需要对 ViewControllerOne 的引用,因为它需要访问该视图控制器中的 ivar。问题是,我是否会在 ViewControllerTwo 中保留 ViewControllerOne@property (retain) ViewControllerOne *vc)?导航控制器已经保留了 ViewControllerOne ,所以我不太确定。

谢谢

I have a scenario that I'm not sure whether to retain a view controller or not. Say I had ViewControllerOne. When a button was pressed, ViewControllerOne would push ViewControllerTwo in the navigation stack. ViewControllerTwo needs a reference to ViewControllerOne because it needs to access an ivar in that view controller. The question is, would I retain ViewControllerOne in ViewControllerTwo (@property (retain) ViewControllerOne *vc) or not? The navigation controller already retains the ViewControllerOne so I'm not really sure.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当梦初醒 2024-09-15 10:40:41

您可能应该将 viewControllerOne 设置为 viewControllerTwo 的委托。一般情况下,不保留代表。

正如您所描述的,viewControllerTwo 不需要特别了解有关 viewControllerOne 的任何信息,它只需要知道如何访问一段数据并通知更高级别的对象该数据的更改。这是代表的常见角色。

You should probably set viewControllerOne up as a delegate of viewControllerTwo. In general, a delegate is not retained.

As you have described it, viewControllerTwo does not need to know anything about viewControllerOne in particular, it just needs to know how to access a piece of data and inform a higher level object of changes to that data. That is a common role for a delegate.

逆流 2024-09-15 10:40:41

问题是:为什么 ViewControllerTwo 需要从 ViewControllerOne 访问 ivar?

更好的方法是在将 ViewControllerTwo 推入堆栈之前,将 ViewControllerOne 中的 ivar 值传递给 ViewControllerTwo

The question is: Why does ViewControllerTwo need to access an ivar from ViewControllerOne ?

The better approach would be to pass the value of the ivar in ViewControllerOne to ViewControllerTwo before pushing ViewControllerTwo to the stack.

明媚殇 2024-09-15 10:40:41

兰普拉戈是正确的。

最好的解决方案是向 ViewControllerTwo 添加一个属性,并将 iVar 从 ViewControllerOne 传递给它,然后再将其推送到导航堆栈。

按照惯例,如果 ViewControllerTwo 是一个对象,则应保留添加到 ViewControllerTwo 的属性,尽管在此示例中您可以使用指定的属性,因为 ViewControllerTwo 将在 ViewControllerOne 之前释放,并且我假设 ViewControllerOne 已保留

ObjectiveC 中的 iVar 所有变量对象通过引用原语传递,结构体通过值传递,就像在 C 中一样

Ranpraygo is correct.

The best solution is to add a property to ViewControllerTwo and pass the iVar from ViewControllerOne to it before pushing it to the navigation stack.

By convention the property added to ViewControllerTwo should be retained if it is an object although you could get away with an assigned property in this example because the ViewControllerTwo will be released before ViewControllerOne is and I assume ViewControllerOne has retained the iVar

In objectiveC all variables that are objects are passed by reference primitives and structs are passed by value as they would be in C

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文