Home > database >  [UINavigationController selectedIndex]: unrecognized selector sent to instance of UITabBarController
[UINavigationController selectedIndex]: unrecognized selector sent to instance of UITabBarController

Time:06-23

While navigating from one screen to another screen(Instance/SubScreen of TabBarController), app is getting crashed and throwing error.

Thread 1: "-[UINavigationController selectedIndex]: unrecognized selector sent to instance 0x105870800"

 UINavigationController *nav_1 = [UIApplication sharedApplication].keyWindow.rootViewController;
    
 UITabBarController *tab = (TabbarController*)[UIApplication sharedApplication].keyWindow.rootViewController;
 NSInteger sel_idx =  tab.selectedIndex; //App Crashing here with error:- Thread 1: "-[UINavigationController selectedIndex]: unrecognized selector sent to instance 0x105870800"
 UINavigationController *nav = [tab.viewControllers objectAtIndex:sel_idx];
  • Before it's working fine when screen redirecting with below code, but facing error of Multiple Navigation Stack, which caused unwanted crashes and app freezing issue
 if ((UserDefaults.standard.value(forKey: "isUserSignIn") as? Bool) == true)
 {
     NSLog("if called tabBarController")
     self.window?.rootViewController = storyBoard.instantiateViewController(withIdentifier: "tabBarController")
 } 
 else
 {
    self.window?.rootViewController = storyBoard.instantiateViewController(withIdentifier: "ProfileNavController")                                                          
 }
  • Now I've changed code as below, bcz I'm facing issue of Multiple Navigation Stack. So after changing to below code Multiple Navigation Stack issue has been resolved but facing mentioned error
if ((UserDefaults.standard.value(forKey: "isUserSignIn") as? Bool) == true)
{
   NSLog("if called tabBarController")
   let tabBar = storyBoard.instantiateViewController(withIdentifier: "tabBarController")
   nav = UINavigationController(rootViewController: tabBar)
   self.window?.rootViewController = nav
} 
else
{
               
  NSLog("else called LoginViewController")                    
  let login = storyBoard.instantiateViewController(withIdentifier: "LoginViewController")
  nav = UINavigationController(rootViewController: login)
  self.window?.rootViewController = nav
                                       
}
  • Crashing with error

error image

CodePudding user response:

To access the tab after you changed the root to a navigation you need

 UINavigationController *nav_1 = [UIApplication sharedApplication].keyWindow.rootViewController;
 UITabBarController *tab = [nav_1.viewControllers objectAtIndex:0]
  • Related