存储标签栏标签核心数据中的顺序

时间:2015-03-22 20:27:56

标签: ios objective-c core-data uitabbarcontroller

我有一个基于tabbar的应用程序,我在这里添加控制器:

UIStoryboard *storyboard = self.storyboard;
self.tabbar = [[UITabBarController alloc]init];

// FirstViewController
SoccerViewController *fvc=[storyboard  instantiateViewControllerWithIdentifier:@"SoccerViewController"];
fvc.navigationItem.title =  @"Fodbold";
fvc.tabBarItem.image=[UIImage imageNamed:@"fodbold.png"];

//SecondViewController
HaandballViewController *svc=[storyboard  instantiateViewControllerWithIdentifier:@"HaandballViewController"];
svc.title=@"Håndbold";
svc.tabBarItem.image=[UIImage imageNamed:@"handball.png"];

//ThirdViewController
FootballViewController *tvc= [storyboard  instantiateViewControllerWithIdentifier:@"FootBallViewController"];
tvc.title=@"Amr. Fodbold";
tvc.tabBarItem.image=[UIImage imageNamed:@"nfl.png"];

//ForthViewController
BasketballViewController *fovc=[storyboard  instantiateViewControllerWithIdentifier:@"BasketBallViewController"];
fovc.title=@"Basketball";
fovc.tabBarItem.image=[UIImage imageNamed:@"basketball.png"];

//FifthViewController
HockeyViewController *ftvc=[storyboard  instantiateViewControllerWithIdentifier:@"HockeyViewController"];
ftvc.title=@"Ishockey";
ftvc.tabBarItem.image=[UIImage imageNamed:@"icehockey.png"];



[API sharedInstance].allViewCont = [NSMutableArray arrayWithObjects:fvc,svc,tvc,fovc,ftvc,  nil];


self.tabbar.viewControllers=  [API sharedInstance].allViewCont;

[self.view addSubview:self.tabbar.view];

 // NSLog(@"%@", [API sharedInstance].allViewCont);

 //[self performSelector:@selector(hideSports) withObject:nil afterDelay:1];

self.tabbar.delegate = self;

现在保存工作,我关闭应用程序并重新打开我得到这个:

"<HaandballViewController: 0x7fdd9a589bd0>",
"<SoccerViewController: 0x7fdd9a58ac40>",
"<FootballViewController: 0x7fdd9a593180>",
"<BasketballViewController: 0x7fdd9a594350>",
"<HockeyViewController: 0x7fdd9a58efe0>"

但我不能像这样设置排序的水龙头:

- (void)setTabOrderIfSaved {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"sortetTabs"];
NSMutableArray *savedOrder = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSMutableArray *orderedTabs = [NSMutableArray arrayWithCapacity:6];
  NSLog(@"Test af gem array %@",savedOrder);
if (savedOrder!=nil && [savedOrder count] > 0 ) {
    for (int i = 0; i < [savedOrder count]; i++){
        for (UIViewController *aController in self.tabbar.viewControllers) {
            if ([aController.title isEqualToString:[savedOrder objectAtIndex:i]]) {
                [orderedTabs addObject:aController];
            }
        }
    }
    for (UIViewController *aController in self.tabbar.viewControllers) {
        int loaded=0;
        for (UIViewController *bController in orderedTabs) {
            if ([aController.title isEqualToString:bController.title]) {
                loaded=1; 
                break; 
            } 
        } 
        if (loaded==0) { 
            [orderedTabs addObject:aController]; 
        } 
    } 

    self.tabbar.viewControllers = orderedTabs;
} 

我修理了这个.......

1 个答案:

答案 0 :(得分:0)

核心数据绝对是存储用户偏好的过度杀伤力。您应该使用NSUserDefaults。只需存储一组描述控制器的字符串(您可以使用您提供的那些可爱的不可读缩写)。

[[NSUserDefaults standardUserDefaults] setObject:
  @[@"fvc", @"svc", @"tvc", @"fovc", @"ftvc"] forKey:@"TabsOrder"];

所以从默认值中获取可变数组:

NSMutableArray *tabs = [NSMutableArray arrayWithArray:
   [[NSUserDefaults standardUserDefaults] objectForKey:@"TabsOrder"]];

编辑完成后存储:

[[NSUserDefaults standardUserDefaults] setObject:tabs forKey:@"TabsOrder"];