2014-06-29 38 views
0

我正在处理选项卡式应用程序,其中一个选项卡显示有关当前邻居的信息(如果用户在此特定区域内)。如果用户离开该区域,则该标签应该被移除,如果他进入该区域,该标签应该被再次添加到TabBar。隐藏并将选项卡添加到UITabbar

我使用CLLocation来计算用户是在区域内还是区域外。但我无法删除并重新添加标签算账:

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完成并使用Storyboards现在链接。

回答

1

变化的代码的最后一行:

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

到:

[self setViewControllers:controllersToKeep animated:YES]; 

因为selfUITabBarController

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]; 
    } 
}