隐藏Tab并将其添加到UITabbar

时间:2014-06-29 18:38:21

标签: ios tabs uitabbar

我正在使用选项卡式应用程序,其中一个选项卡显示有关当前社区的信息(如果用户位于此特定区域内)。如果用户离开该区域,则应删除该选项卡,如果他进入该区域,则应将该选项卡再次添加到TabBar。

我实现了使用CLLocation计算用户是在区域内还是在区域外。但我之后无法删除并再次添加Tab:

UITabBarController.m:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    currentposition = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
    [self userisincity];
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    [self userisincity];
}


- (void)userisincity
{
    if ((currentposition.coordinate.longitude > 17.50) && (currentposition.coordinate.longitude < 17.70) &&  (currentposition.coordinate.latitude > 37.45) && (currentposition.coordinate.latitude < 37.65)){

        NSLog(@"inside city");
        //add tab

    } else {

        NSLog(@"outside city");

        //remove tab
        NSUInteger indexToRemove = 0;
        NSMutableArray *controllersToKeep = [NSMutableArray arrayWithArray:self.viewControllers];

        UIViewController *removedViewController = [controllersToKeep objectAtIndex:indexToRemove];

        [controllersToKeep removeObjectAtIndex:indexToRemove];

        NSLog(@"%@", controllersToKeep);

        [self.tabBarController setViewControllers:controllersToKeep animated:YES];
    }

}

日志显示用户是在内部还是外部,以便位置部件正常工作。 controllersToKeep首先有4个条目,其中一个被删除。但是setViewControllers没有效果。

如何再次添加标签?它是一个ViewController,现在使用Storyboard完成并链接。

2 个答案:

答案 0 :(得分:1)

更改最后一行代码:

[self.tabBarController setViewControllers:controllersToKeep animated:YES];

为:

[self setViewControllers:controllersToKeep animated:YES];

因为selfUITabBarController

答案 1 :(得分:0)

此解决方案到目前为止工作: 我还必须在添加/删除选项卡后更新选项卡的名称。

if ((currentposition.coordinate.longitude > XY) && (currentposition.coordinate.longitude < XY) &&  (currentposition.coordinate.latitude > XY) && (currentposition.coordinate.latitude < XY)){  
   //add view
   NSMutableArray *ViewControllers = [NSMutableArray arrayWithArray:self.viewControllers];

   if (ViewControllers.count == 3) {
       UINavigationController *nextomeNavigationController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerID"];
       //set the name of your Storyboard containing the ViewController and the ID you gave to the ViewController here
       [ViewControllers insertObject:nextomeNavigationController atIndex:0];
       [self setViewControllers:ViewControllers animated:YES];
   }

} else {
   //remove view
   NSMutableArray *ViewControllers = [NSMutableArray arrayWithArray:self.viewControllers];

   if (ViewControllers.count == 4) {
       [ViewControllers removeObjectAtIndex:0];
       [self setViewControllers:ViewControllers animated:YES];
   }
}