我用故事板构建了我的应用程序,所有视图都由 tabbarcontroller 管理。
所以在启动时(我目前只在 iPad UI 上工作)它这样做:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UISplitViewController *splitViewController = [tabBarController.viewControllers objectAtIndex:0];
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
ProductionMasterViewController *controller = (ProductionMasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
}
}
我希望能够根据用户输入启用或禁用 tabBarController 中的选项卡(例如,需要在第一个选项卡中选择一个项目才能访问默认情况下禁用的第二个和第三个选项卡)
我不清楚的是如何访问选项卡以启用/禁用它们。我会创建一个 appdelegate 的实例,然后做类似的事情吗
AppDelegate *d = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabs = (UITabBarController *)[d.window rootViewController];
[[[[tabs tabBar] items] objectAtIndex:2] setEnabled:YES];
[[[[tabs tabBar] items] objectAtIndex:3] setEnabled:YES];
[[[[tabs tabBar] items] objectAtIndex:4] setEnabled:YES];
(这有点像它应该工作,但它也似乎相当恶心。)