应用程序终止时如何保存标签栏顺序?
在 iOS4 中不起作用。
我希望能够在应用程序关闭时保存选项卡的顺序。
The following example:
http://www.raddonline.com/blogs/geek-journal/iphone-sdk-uitabbarcontroller-how-to-save-user-customized-tab-order/
Doesn't work in iOS4.
I would like to be able to save the order of my tabs when the application is shut down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将应用程序委托设置为
UITabBarController
委托,并且当选择选项卡时,您可以将选择记录为[NSUserDefaults standardDefaults]
作为整数(选定的索引)标签栏控制器)。执行此操作:
启动时,您可以使用
-integerForKey:
从默认值加载所选索引,如果未找到该键的值,或者该键未解析为,则默认值为零一个整数。我使用以下方法包装对已保存索引的访问:
关于 NSUserDefaults 的说明.. 在它们同步之前,它们不会刷新到磁盘以实现持久性。如果您正在调试并终止应用程序,并且 iOS 尚未自动同步应用程序的默认设置,您会注意到这一点。您可以强制同步,但我更喜欢保留它直到它自动完成。为了帮助解决用户切换选项卡然后退出的情况,请将同步添加到
- (void)applicationWillResignActive:(UIApplication *)application
和/或- (void)applicationWillTerminate:(UIApplication * )应用程序
。指的是制表符顺序,而不是选定的索引(我的错!)。
您可以简单地为每个选项卡栏项目设置一个唯一的标签号。 (在应用程序委托中使用枚举来保持它们的唯一性)。然后您可以这样做:
要恢复启动时的顺序,您可以按照从默认值恢复的顺序手动组织选项卡栏项目,然后设置
UITabBar
items 属性> 直接。You can set your Application delegate to also be a
UITabBarController
delegate, and when the tab is selected, you can record the selection to[NSUserDefaults standardDefaults]
as an integer (selected index of tab bar controller).Do this in:
On launch, you can load the selected index from defaults using
-integerForKey:
and it will default to zero if a value was not found for that key, or if the key was not parsed as an integer.I wrap the access to the saved index with a method:
A note about
NSUserDefaults
.. until they are syncronized, they are not flushed to disk for persistence. You will notice this if you're debugging and terminate the app, and iOS hasn't automatically synchronized your app's defaults. You can force synchronize, but I prefer to leave it until it's done automatically. To help with cases where the user switches tab, and then quits, add synchronize to- (void)applicationWillResignActive:(UIApplication *)application
and/or- (void)applicationWillTerminate:(UIApplication *)application
.Referring to the tab order, rather than selected index (my bad!).
You can simply set each of your tab bar items with a unique tag number. (use an enumeration in your app delegate to keep them unique). Then you can do this:
To restore the order at launch, you can manually organize the tab bar items in the order restored from defaults, and then set the
items
property of theUITabBar
directly.如何:自定义选项卡时保存选项卡顺序在 UITabBarController - 我遵循了这个,然后我们就开始了。
How to: Save order of tabs when customizing tabs in UITabBarController - i followed this and off we go.