如何设置 NSMutableArray 以便从不同选项卡访问
我有一个带有标签栏的应用程序。每个选项卡都有自己的视图控制器。我还有一个可变数组,每个选项卡视图控制器都应该能够访问它(获取第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我面临着类似的问题。环顾四周后,我决定使用一个单例,在其 @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