我正在处理选项卡式应用程序,其中一个选项卡显示有关当前邻居的信息(如果用户在此特定区域内)。如果用户离开该区域,则该标签应该被移除,如果他进入该区域,该标签应该被再次添加到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现在链接。