如何设置 NSMutableArray 以便从不同选项卡访问

发布于 2024-10-17 19:35:50 字数 379 浏览 2 评论 0原文

我有一个带有标签栏的应用程序。每个选项卡都有自己的视图控制器。我还有一个可变数组,每个选项卡视图控制器都应该能够访问它(获取第 i 个元素、添加新元素、删除元素等)。现在,我正在做一个丑陋的黑客,当我在 viewWillAppear 和 viewDidDisappear 方法中使用类似的东西更改选项卡时,它会传递数组:

[[[[self.tabBarController viewControllers] objectAtIndex:2] viewControllers] objectAtIndex:0];

请有人告诉我执行此操作的正确方法?我有一种感觉,我应该将数组保留在我的应用程序委托中,但我不确定如果这样做,如何从其他视图控制器访问它。

非常感谢!

I have an app with a tab bar. Each tab has its own view controller. I also have a mutable array that each tab view controller should be able to access (get the ith element, add a new element, delete an element, etc). Right now, I'm doing an ugly hack, which is passing the array around as I change tabs with using things like this in the viewWillAppear and viewDidDisappear methods:

[[[[self.tabBarController viewControllers] objectAtIndex:2] viewControllers] objectAtIndex:0];

Please can someone enlighten me as to the correct way to do this? I have a feeling I should just keep the array in my appdelegate, but I'm not sure how to access it from the other view controllers if I do that.

Many thanks!

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

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

发布评论

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

评论(1

£烟消云散 2024-10-24 19:35:50

我面临着类似的问题。环顾四周后,我决定使用一个单例,在其 @interface 中声明 NSMutableArray,然后在所有 viewController 之间共享。
我从此页面开始了解一般形状。然后我传递调用来设置数组的值。我还没有 100% 完成(参见 我的这篇文章)但我认为这应该是这样。任何其他想法都非常受欢迎!
祝你今天过得愉快!
/卢卡

I'm facing a similar issue. After looking around I decided for a singleton which declares the NSMutableArray in its @interface, and then is shared between all the viewControllers.
I started from this page for the general shape. I'm then passing around calls to set up the values of the array. I'm not still 100% done (see this post of mine) but I think this should be the way. Any other ideas are most than welcome!
Have a nice day!
/Luca

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