moreViewController, который появляется в панели вкладок с более чем 5 элементами, кажется, корректно обрабатывает контроллеры представления, но не контроллеры навигации. Кто-нибудь может объяснить, почему это так?
Вот как воспроизвести проблему в прозе, затем в коде:
Я создаю простое приложение с 6 UIViewControllers в 6 вкладках. Поскольку у меня более 5 вкладок, вкладки «5» и «6» находятся в списке moreNavigationList.
6-я вкладка содержит кнопку, которая при нажатии удаляет первую вкладку. Это уменьшает количество вкладок до 5, поэтому moreNavigationController больше не требуется и исчезает. Вкладка «6» теперь перемещается в последнюю точку панели вкладок. Все как и ожидалось.
Теперь, если поместить контроллер вида из вкладки «6» (то есть тот, что с кнопкой) в контроллер навигации, все будет сломано. Если я нажимаю кнопку, вкладка «1» удаляется из панели вкладок, moreNavigationController исчезает, и вкладка «6» теперь отображается в последнем месте панели вкладок. Однако его содержание ушло. Нет кнопки, нет ничего.
Из анализа иерархии представлений, похоже, происходит то, что moreNavigationController удаляет контроллер представления «6» из своего исходного контроллера навигации в [tabBarController viewControllers] и добавляет его в свой стек. Но, похоже, его не вернуть, когда исчезнет контроллер moreNavigationController.
Вот код, который я использовал для воспроизведения этого в простом оконном тестовом приложении:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create a tab bar with 5 regular view controllers and a navigation controller
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
UIViewController* vc1 = [[[UIViewController alloc] init] autorelease]; vc1.title = @"1";
UIViewController* vc2 = [[[UIViewController alloc] init] autorelease]; vc2.title = @"2";
UIViewController* vc3 = [[[UIViewController alloc] init] autorelease]; vc3.title = @"3";
UIViewController* vc4 = [[[UIViewController alloc] init] autorelease]; vc4.title = @"4";
UIViewController* vc5 = [[[UIViewController alloc] init] autorelease]; vc5.title = @"5";
UIViewController* vc6 = [[[UIViewController alloc] init] autorelease]; vc6.title = @"6";
// Add a button that removes tab "1" when pressed to vc6
UIButton *moveButton = [self moveButton];
[vc6.view addSubview:moveButton];
vc6.view.backgroundColor = [UIColor greenColor];
moveButton.center = vc6.view.center;
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:vc6] autorelease];
// Everything is fine if vc6 is added directly instead of inside a navigation controller
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, vc4, vc5, navController, nil];
tabBarController.viewControllers = controllers;
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (UIButton *)moveButton
{
UIButton *moveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
moveButton.frame = CGRectMake(0, 0, 150, 50);
[moveButton setTitle:@"Remove 1" forState:UIControlStateNormal];
[moveButton addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
return moveButton;
}
- (void)remove
{
// remove 1st tab bar item (this also removes moreNavigationController)
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
[viewControllers removeObjectAtIndex:0];
[tabBarController setViewControllers:viewControllers];
}
SDK используется 4,3