gpt4 book ai didi

iphone - 如何: Save order of tabs when customizing tabs in UITabBarController

转载 作者:行者123 更新时间:2023-12-03 18:27:49 25 4
gpt4 key购买 nike

除了有关如何保存 UITabBarController 的 Tab 键顺序的文档之外,我无法找到任何其他信息,以便保存用户的自定义以供下次应用程序启动。我在网上搜索过,但找不到任何通过正确代码执行此操作的博客文章或文章。

我意识到我必须使用 UITabBarController (didEndCustomizingViewControllers:) 的委托(delegate)方法,但我不确定如何在保存用户想要选项卡的顺序状态方面最好地实现持久性。

有人可以发布一些代码,为我指出正确的方向,或者您可能有已保存内容的链接吗? :)

谢谢

最佳答案

就您要求的一些示例代码而言,我将简单地在此处发布我如何在应用程序中处理相同的任务。

快速介绍:我使用 NIB 文件来存储初始 UITabBarController 状态,并且为了使我的选项卡与另一个选项卡不同,我只需为分配给每个选项卡的 UITabBarItem 对象定义标签变量UIViewController 填充在我的 UITabBarController 中。为了能够准确跟踪最后选定的选项卡(包括“更多”选项卡),我为 UITabBarControllerUINavigationControllerDelegateUITabBarControllerDelegate 实现了以下方法> 其 moreNavigationController。它们在这里:

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[[NSUserDefaults standardUserDefaults] setInteger:mainTabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[[NSUserDefaults standardUserDefaults] setInteger:mainTabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"];
}

#pragma mark UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[[NSUserDefaults standardUserDefaults] setInteger:tabBarController.selectedIndex forKey:@"mainTabBarControllerSelectedIndex"];
}

这是保存选项卡顺序的代码:

#pragma mark UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
int count = mainTabBarController.viewControllers.count;
NSMutableArray *savedTabsOrderArray = [[NSMutableArray alloc] initWithCapacity:count];
for (int i = 0; i < count; i ++) {
[savedTabsOrderArray addObject:[NSNumber numberWithInt:[[[mainTabBarController.viewControllers objectAtIndex:i] tabBarItem] tag]]];
}
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:savedTabsOrderArray] forKey:@"tabBarTabsOrder"];
[savedTabsOrderArray release];
}

如您所见,我一直将选项卡索引的顺序存储在 NSUserDefaults 的数组中。

applicationDidFinishLaunching: 方法中启动应用程序时,我使用以下代码重新排序了 UIViewControllers:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
mainTabBarController.delegate = self;

int count = mainTabBarController.viewControllers.count;
NSArray *savedTabsOrderArray = [[userDefaults arrayForKey:@"tabBarTabsOrder"] retain];
if (savedTabsOrderArray.count == count) {
BOOL needsReordering = NO;

NSMutableDictionary *tabsOrderDictionary = [[NSMutableDictionary alloc] initWithCapacity:count];
for (int i = 0; i < count; i ++) {
NSNumber *tag = [[NSNumber alloc] initWithInt:[[[mainTabBarController.viewControllers objectAtIndex:i] tabBarItem] tag]];
[tabsOrderDictionary setObject:[NSNumber numberWithInt:i] forKey:[tag stringValue]];

if (!needsReordering && ![(NSNumber *)[savedTabsOrderArray objectAtIndex:i] isEqualToNumber:tag]) {
needsReordering = YES;
}
}

if (needsReordering) {
NSMutableArray *tabsViewControllers = [[NSMutableArray alloc] initWithCapacity:count];
for (int i = 0; i < count; i ++) {
[tabsViewControllers addObject:[mainTabBarController.viewControllers objectAtIndex:
[(NSNumber *)[tabsOrderDictionary objectForKey:
[(NSNumber *)[savedTabsOrderArray objectAtIndex:i] stringValue]] intValue]]];
}
[tabsOrderDictionary release];

mainTabBarController.viewControllers = [NSArray arrayWithArray:tabsViewControllers];
[tabsViewControllers release];
}
}
[savedTabsOrderArray release];

if ([userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"]) {
if ([userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"] == 2147483647) {
mainTabBarController.selectedViewController = mainTabBarController.moreNavigationController;
}
else {
mainTabBarController.selectedIndex = [userDefaults integerForKey:@"mainTabBarControllerSelectedIndex"];
}
}

mainTabBarController.moreNavigationController.delegate = self;

[window addSubview:mainTabBarController.view];
}

这非常棘手,可能看起来很奇怪,但不要忘记我的 UITabBarController 是完全在 nib 文件中创建的。如果您以编程方式构建它,您可以简单地执行相同的操作,但遵循保存的顺序。

P.S.:并且不要忘记在应用终止时同步 NSUserDefaults

- (void)applicationWillTerminate:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] synchronize];
}

我希望这会有所帮助。如果有不清楚的地方请评论并提问。

关于iphone - 如何: Save order of tabs when customizing tabs in UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2034183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com