#ios #tabs #uitabbar
#iOS #вкладки #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) amp;amp; (currentposition.coordinate.longitude < 17.70) amp;amp; (currentposition.coordinate.latitude > 37.45) amp;amp; (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, созданный и связанный с помощью раскадровки прямо сейчас.
Ответ №1:
Измените последнюю строку кода с:
[self.tabBarController setViewControllers:controllersToKeep animated:YES];
Для:
[self setViewControllers:controllersToKeep animated:YES];
поскольку self
это UITabBarController
.
Ответ №2:
это решение работает до сих пор: мне также пришлось обновлять названия вкладок после их добавления / удаления.
if ((currentposition.coordinate.longitude > XY) amp;amp; (currentposition.coordinate.longitude < XY) amp;amp; (currentposition.coordinate.latitude > XY) amp;amp; (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];
}
}