如何:获取选项卡顺序,保存到 NSUserDefaults 中然后检索
我有一个带有默认视图和选项卡的UitabBarApplication ...等。我有八个具有可自定义选项的选项卡,因此用户可以重新排序选项卡。
如何获得选项卡的顺序,将其保存到NsuserDefaults中,并在应用程序加载后检索。我想看一些代码示例。
这是我到目前为止的位置:
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:8];
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:8];
NSArray *defaultOrder = tabBarController.viewControllers;
for (int i =0; i < 8; i++){
for (UIViewController *theVC in defaultOrder) {
if ([theVC.title isEqualToString:[tabLayout objectAtIndex:i]]) {
[orderedLayout addObject:theVC];
}
}
}
tabBarController.viewControllers = orderedLayout;
}
此代码似乎在模拟器中不起作用。我重新排序选项卡并完成完成,然后停止应用程序,但是当我点击构建并再次运行时,应用程序还会恢复为默认情况。为什么上面的代码不起作用?
谢谢。
I have a default UITabBarApplication with default views and tabs... etc. I have eight tabs that have the customizable option, so users can reorder tabs.
How do I get the order of the tabs, save that into NSUserDefaults and retrieve it when the app loads back up. I'd like to see some code examples.
Here is where I'm at so far:
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
NSMutableArray *vcArray = [NSMutableArray arrayWithCapacity:8];
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:8];
NSArray *defaultOrder = tabBarController.viewControllers;
for (int i =0; i < 8; i++){
for (UIViewController *theVC in defaultOrder) {
if ([theVC.title isEqualToString:[tabLayout objectAtIndex:i]]) {
[orderedLayout addObject:theVC];
}
}
}
tabBarController.viewControllers = orderedLayout;
}
This code doesn't seem to work in the simulator. I reorder the tabs and hit done then stop the app, but when I hit build and run again the app reverts to default. Why isn't the code above working?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一条评论:
NSUserDefaults
并不总是适用于模拟器。如果您每次都进行新的构建,那么当然一切都会重置。两个建议:
在后一个方法中放置一个
NSLog
以确保它被调用将应用程序构建到设备,移动选项卡,关闭应用程序(完全关闭 - 不仅仅是在后台),再次打开应用程序,看看会发生什么。
One comment:
NSUserDefaults
don't always work with the simulator. If you're doing a new build each time, then of course everything gets reset.Two suggestions:
put an
NSLog
in the latter method to make sure that it's being calledbuild the app to a device, move the tabs around, close the app (completely -- not just in background), open the app again, behold what happens.