如何将选项卡顺序保存到 NSUserDefaults
我有 8 个选项卡,并且正在使用可自定义的 tabBarItems。因此用户可以对选项卡重新排序。现在我的问题是如何获取选项卡的顺序并将其保存到 NSUserDefaults 中,以便每当用户退出应用程序并返回时它都保持不变。
这是我到目前为止得到的代码:
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:8];
NSArray *tabOrderToSave = tabBarController.viewControllers;
for (UIViewController *aViewController in tabOrderToSave) {
[savedOrder addObject:aViewController.title];
}
[[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"];
}
该代码没有错误,只是不起作用。
我做错了什么?
顺便说一句:我的应用程序是 tabBarApplication。
编辑: 这是我所做的。
创建了一个 tabBar 应用程序
使用数据填充视图
添加了 8 个选项卡,其中包含从控制器加载的不同视图
然后添加了可自定义的选项卡重新排序
添加了上面的代码来保存 Tab 键顺序
重新启动应用程序时是否可以检索已保存的数据?
编辑2:
我一直在四处寻找,发现了很多关于这方面的信息,但关于如何进行的信息却很少。
这是我现在拥有的代码:
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:6];
NSArray *savedViews = tabBarController.viewControllers;
for (UIViewController *theVC in savedViews){
[vcArray addObject:theVC.title];
}
[[NSUserDefaults standardUserDefaults] setObject:vcArray forKey:@"tabLayout"];
}
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *tabLayout = [defaults arrayForKey:@"tabLayout"];
NSMutableArray *orderedLayout = [NSMutableArray arrayWithCapacity:6];
NSArray *defaultOrder = tabBarController.viewControllers;
for (int i =0; i < 6; i++){
for (UIViewController *theVC in defaultOrder) {
if ([theVC.title isEqualToString:[tabLayout objectAtIndex:i]]) {
[orderedLayout addObject:theVC];
}
}
}
tabBarController.viewControllers = orderedLayout;
}
为什么这不起作用,我的意思是在我自定义选项卡栏并在 Xcode 中单击停止后,当我再次运行它时,它不会显示我保存的订单。我到底做错了什么?这是执行此操作的正确方法吗?
I have 8 tabs and am using customizable tabBarItems. So the user can reorder the tabs. Now my question is how do I get the order of the tabs and save that to NSUserDefaults so it remains the same whenever the user exits the app and comes back.
Here is the code I've got so far:
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed { NSMutableArray *savedOrder = [NSMutableArray arrayWithCapacity:8]; NSArray *tabOrderToSave = tabBarController.viewControllers; for (UIViewController *aViewController in tabOrderToSave) { [savedOrder addObject:aViewController.title]; } [[NSUserDefaults standardUserDefaults] setObject:savedOrder forKey:@"savedTabOrder"]; }
No errors in that code, it just doesn't work.
What am I doing wrong?
By the way: My app is a tabBarApplication.
EDIT:
Here is what I've done.
Created a tabBar Application
Populated Views with data
Added 8 tabs with different views loaded from controllers
Then added the customizable reordering of tabs
Added the code above to save the tab order
Have do I retrieve that saved data when I relaunch the app?
EDIT 2:
I've been searching around and found a lot of info on this but very little on how.
Here is the code I have now:
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:6];
NSArray *savedViews = tabBarController.viewControllers;
for (UIViewController *theVC in savedViews){
[vcArray addObject:theVC.title];
}
[[NSUserDefaults standardUserDefaults] setObject:vcArray forKey:@"tabLayout"];
}
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *tabLayout = [defaults arrayForKey:@"tabLayout"];
NSMutableArray *orderedLayout = [NSMutableArray arrayWithCapacity:6];
NSArray *defaultOrder = tabBarController.viewControllers;
for (int i =0; i < 6; i++){
for (UIViewController *theVC in defaultOrder) {
if ([theVC.title isEqualToString:[tabLayout objectAtIndex:i]]) {
[orderedLayout addObject:theVC];
}
}
}
tabBarController.viewControllers = orderedLayout;
}
Why doesn't this work, and by that I mean after I customize the tabbar and hit stop in Xcode, when I go to run it again it doesn't show my saved order. What in the world am I doing wrong? Is this the correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你错过了同步。
试试这个:
You're missing the synchronize.
Try this:
正如 @Nekto 所说 - 什么不起作用?你如何检测到这一点?
保存此信息也不会保存您的标签栏顺序...您必须从 nsuserdefault 获取此订单并下次按该顺序生成选项卡..
As @Nekto said - What doesn't work? How do you detect this?
also saving this info won't save your tabbar order... you have to fetch this order from nsuserdefault and generate the tab in that order next time ..
这是我一直在使用的代码
// 注意 - 因为选项卡可能会在应用程序版本之间发生变化,
// 如果选项卡数量或名称不应该尝试恢复选项卡
// 与保存的版本不同。
Here's the code I've been using
// NOTE -- because tabs may change between versions of the app,
// should NOT try to restore tabs if the tab count or names
// differ from the saved version.