UITabbarController динамически меняет элементы

Я хочу редактировать UITabbarItems в течение времени просмотра. Предыстория заключается в том, что у меня есть приложение с видом входа в систему, и в зависимости от того, является ли пользователь администратором или нет, TabbarItem администратора должен быть виден или нет.

Я использую этот код для первоначального создания UITabbarController в AppDelegate:

// AppDelegate.m

settings = [[settingsViewController alloc] init];
settings.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Einstellungen" image:[UIImage imageNamed:@"settings.png"] tag:3];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:readState, friends, settings, nil];

Когда я позже пытаюсь манипулировать элементами из другого UIViewController, ничего не происходит, и UITabbar остается таким же, как и раньше. На самом деле я пробовал два способа, которые мог себе представить:

[[self tabBarController] setToolbarItems:[[[self tabBarController] toolbarItems] arrayByAddingObject:admin] animated:NO];
[[self tabBarController] setViewControllers:[[[self tabBarController] viewControllers] arrayByAddingObject:admin] animated:NO];

Как я могу достичь своей цели? Заранее спасибо, с уважением, Юлиан.


person Julian F. Weinert    schedule 29.06.2012    source источник


Ответы (1)


Я нашел обходной путь для моей проблемы. Мне не очень нравится импортировать AppDelegate, но кажется, что свойство tabbarController не устанавливается автоматически для UIViewControllers UITabbarController.

// generate an instance of the needed UIViewController
adminViewController *admin = [[adminViewController alloc] init];
admin.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Admin" image:[UIImage imageNamed:@"admin.png"] tag:5];

// get the AppDelegate instance
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

// get the »viewControllers« array of the AppDelegates UITabbarController as mutable array
NSMutableArray *viewControllersArray = [NSMutableArray arrayWithArray:[[appDelegate tabBarController] viewControllers]];
// insert the UITabbarItem of the needed UIViewController
[viewControllersArray insertObject:admin atIndex:2];

// Finally tell the app delegates UITabbarController to set the needed viewControllers
[[appDelegate tabBarController] setViewControllers:viewControllersArray animated:NO];
person Community    schedule 29.06.2012