iPhone 开发 - 标签栏视图控制器的缓存值
我有一个根 UIViewController 子类,它有一个 UITabBar(我没有使用 UITabBarController)和 5 个视图控制器的 5 个选项卡栏项目。每个视图控制器都有一个 UIPickerView。一次只有一个视图控制器被实例化——当选择一个选项卡栏项目时,当前的视图控制器被删除,新的视图控制器被实例化并添加,旧的视图控制器被释放(self.oldvc = nil;)。因此,如果用户转到第一个选项卡中选择器的第三行,然后切换到第二个选项卡,然后返回,我希望再次选择选择器的第三行。目前,我拥有它,因此视图控制器使用 initWithSelectedRowOfFirstComponent:(NSInteger)firstRow secondaryComponent:(NSInteger).. 等进行实例化,然后在我的根视图控制器(带有选项卡栏的 oner)中,我有一个 ivar缓存每个值..但我不确定..有更好的方法来做到这一点吗?也许是一个 plist 文件或一本字典或其他东西..我不知道,你认为最好的方法是什么?谢谢!!
我决定使用 UITabBarController,让内存警告解除分配并使用一个我称为“Model”的类,它是一个单例(使用 此宏)用于保存视图因内存警告而被卸载时使用的数据。
I have a root UIViewController subclass that has a UITabBar, (I'm not using UITabBarController) and 5 tab bar items for 5 view controllers. Each view controller has a UIPickerView. Only one view controller is ever instantiated at a time -- when a tab bar item is selected, the current one is removed, the new one is instantiated and added, and the old one is deallocated (self.oldvc = nil;). So if the user goes to the 3rd row of the picker in the first tab, then switches to the second tab, then goes back, I want the 3rd row of the picker to be selected again. Currently, I have it so the view controllers are instantiated with a initWithSelectedRowOfFirstComponent:(NSInteger)firstRow secondComponent:(NSInteger).. etc., and then in my root view controller, (the oner with the tab bar), I have an ivar to cache each of the values.. But I'm not sure.. Is there a better way to do this? Maybe a plist file or a dictionary or something.. I don't know, what do you think the best way to do this would be? Thanks!!
I decided to use UITabBarController, let memory warnings deallocate and use a class I called "Model" which is a singleton (using this macro) to hold the data to use when the views get unloaded because of memory warnings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

如果您不想保留分配的所有五个视图控制器(在数组中),您可以将视图的状态分离到一个新类中。然后,您将保留该类的 5 个实例,并编写一个函数,该函数可以从新类的实例初始化您的 UIPickerViews。
每当用户选择不同的选项卡时,您都会将对相应状态对象的引用传递给设置视图的函数。此外,通过视图对状态所做的所有更改都应直接应用于模型对象。您的根 UIViewController 充当主控制器。另请参阅:MVC 模式。
基于您的评论的其他想法...
我认为您可以尝试以下操作:不要在按下选项卡按钮时创建和销毁 UIPickerView 对象,而是按住这些对象。在主控制器中有一个“NSMutableArray *viewArray”来保存所有视图。我假设您的根 UIViewController 是 UITabBar 的委托。当您收到 tabBar:didSelectItem: 消息时,您将隐藏当前视图并显示与所选项目相对应的视图。只需迭代整个数组并根据项目是否对应于选定的选项卡栏项目来隐藏/取消隐藏该项目。
基本上,这有点像开始实现您自己的 UITabBarController 版本,所以也许您应该考虑使用它。
If you don't want to keep all of the five view controllers allocated (in an array) you could separate the state of your views into a new class. You'd then keep 5 instances of that class around and write a function that can initialize your UIPickerViews from an instance of your new class.
Whenever the user selects a different tab, you pass a reference to the corresponding state object to the function that sets up your view. Also, all changes made to the state through the view should be directly applied to the model objects. Your root UIViewController acts as the main controller. See also: MVC Pattern.
Additional thoughts based on your comments...
I think you can try the following: Rather than creating and destroying your UIPickerView objects whenever a tab button is pressed, hold on to the objects. Have an "NSMutableArray *viewArray" in your main controller that holds all the views. I assume your root UIViewController is the delegate of the UITabBar. When you receive a tabBar:didSelectItem: message, you hide the current view and display the one corresponding to the selected item. Just iterate over the entire array and hide/unhide the item based on whether it corresponds to the selected tab bar item.
Basically this is a bit like starting to implement your own version of UITabBarController, so maybe you should consider using that.