1

ビューの存続期間中にUITabbarItemsを編集したい。背景には、ログインビューを備えたアプリがあり、ユーザーが管理者であるかどうかに応じて、管理者のTabbarItemが表示されるかどうかがわかります。

私はこのコードを使用して、最初にAppDelegateにUITabbarControllerを作成しています。

// 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は以前と同じままです。私は実際に私が想像できる2つの方法を試しました:

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

どうすれば目標を達成できますか?よろしくお願いします、ジュリアン

4

1 に答える 1

4

私は自分の問題の回避策を見つけました。AppDelegateをインポートするのはあまり好きではありませんが、UITabbarControllerのUIViewControllersに対してtabbarControllerプロパティが自動的に設定されていないようです。

// 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];
于 2012-06-29T23:35:17.203 に答える