The image of the UITabBar MoreViewController in the UITableView disappears after programmatically selecting it

I am trying to select one of my UINavigationControllers in an array of UITabBar.viewControllers.

I previously tried it with the UITabbarController.selectedIndex setting, but the Apple documentation says: "To select the More navigation controller itself, you must change the value of the selectedViewController property."

AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; UINavigationController *navController = [appDelegate.objTabBarController.viewControllers objectAtIndex:5]; [appDelegate.objTabBarController setSelectedViewController:navController]; 

Doing this is good, but when I go back to the MoreViewController list view, the icon on the left side will disappear and not return.

Everything is fine when you select it with your finger.

Selecting is fine

Error choosing programmatically β†’ image is gone

Per Code Step 1Per Code Step 2Per Code Step 3

Any suggestions what am I doing wrong?

Regards, Steve

+7
source share
3 answers

Choosing an optional navigation controller

To select moreNavigationController, you must set selectedViewController :

 self.tabBarController.selectedViewController = self.tabBarController.moreNavigationController; 

The reason is that it is simple: the NavigationController is no longer inside the viewControllers the tab bar controller, so it cannot be selected by index.

To select one of the "more" controllers directly, you can set the selectedViewController property:

 self.tabBarController.selectedViewController = viewController5; 

Or you can install selectedIndex :

 self.tabBarController.selectedIndex = 5; 

Locking the displayed tab bar

The disappearing image of the tab bar is caused by the use of the navigation controller (for settings) inside the navigation controller ( moreNavigationController generated by the tab bar controller). To fix this, there are two solutions:

  • Do not add navigation controllers for a larger section, but add controllers directly. The structure of the controller will look like this, assuming that the controllers in all tabs need navigation:

     tabBarController + navigationController + viewController0 + navigationController + viewController1 + navigationController + viewController2 + navigationController + viewController3 + viewController4 + viewController5 
  • Install the tabBarItem controller tabBarItem on your navigation controller (you only need to do this once):

     UINavigationController *settingsNavigationController = [appDelegate.objTabBarController objectAtIndex:5]; UIViewController *settingsRootController = settingsNavigationController.viewControllers[0]; settingsNavigationController.tabBarItem = settingsRootController.tabBarItem; 
+1
source

I ran into this problem. It seems that the navigation controller somehow lost control of the controller. In my case, the navigation controller whose image was disappearing was at index 7 in the tabBarController. The navigation controller should have contained a viewController of the SettingsViewController class. But sometimes he lost it.

To fix this, I had to add code to two classes - the application delegate and SettingsViewController. In the application delegate:

 -(void) fixSettingsNavigationController { UITabBarController* tab = self.tabBarController; NSArray* vcs = tab.viewControllers; NSInteger nVCs = vcs.count; if (nVCs > 7) { UIViewController* settingsContainer = vcs[7]; UINavigationController* settingsContainerNav = (UINavigationController*)settingsContainer; if ([settingsContainerNav isKindOfClass: [UINavigationController class]]) { NSArray* settingsNavVCs = settingsContainerNav.viewControllers; NSInteger count = settingsNavVCs.count; SettingsViewController* svc = self.settingsViewController; if (!count) { // The container navigation controller has lost track of the settings view controller. settingsContainerNav.viewControllers = [NSArray arrayWithObject:svc]; } } } } 

And in SettingsViewController:

 -(void) viewWillDisappear:(BOOL)animated { MyAppDelegate* appDel = [[UIApplication sharedApplication] delegate]; [appDel fixSettingsNavigationController]; [super viewWillDisappear: animated]; } 
0
source

I came across a very similar situation where I want to programmatically select the View controller inside the MoreViewController.

As you said in your question, the apple does not allow you to select a view controller that is inside the MoreViewController (or does not appear on the tab).

In my case, I don’t need the standard More functions to reorder tabs on a tab.

So, I did the following things

  • Created UITableViewControlelr as my fifth tab, set its title to larger and custom image (looks like the default image is larger.)

  • In my UITableViewController, I have listed the elements corresponding to ViewControllers. Clicking on which displays the corresponding view controller.

  • When I need to display any view controller that is inside my UITableViewController programmatically, I just set a flag and click on my UITableViewController, and the viewDidAppear method of my UITableViewController clicks on the required view controller.

  • It will also display a back button on a programmatically open view in the MoreViewController list.

0
source

All Articles