[UINavigationController selectedIndex]:发送到 UITabBarController 实例的无法识别的选择器

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

提问人:steveSarsawa 提问时间:6/23/2022 最后编辑:Shehata GamalsteveSarsawa 更新时间:6/23/2022 访问量:142

问:

从一个屏幕导航到另一个屏幕(TabBarController 的实例/子屏幕)时,应用程序崩溃并引发错误。

线程 1:“-[UINavigationController selectedIndex]:发送到实例 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];
  • 之前,当使用以下代码重定向屏幕时,它可以正常工作,但遇到错误,这导致了不必要的崩溃应用程序冻结问题Multiple Navigation Stack
 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")                                                          
 }
  • 现在我已经更改了代码,如下所示,bcz 我遇到了 .因此,在更改为以下代码后,问题已解决,但面临提到的错误Multiple Navigation StackMultiple Navigation Stack
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
                                       
}
  • 崩溃并出现错误

error image

iOS Objective-C UIAningController,UITabbarController

评论


答:

1赞 Shehata Gamal 6/23/2022 #1

若要在将根目录更改为导航后访问该选项卡,需要

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