除UINavigationController

除UINavigationController

This question already has answers here:
Removing NavigationController Programmatically
                                
                                    (2个答案)
                                
                        
                                2年前关闭。
            
                    
我必须从我的应用中删除UINavigationController以添加UITabBarController并保留
我的if statement照常工作。

我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    if UserDefaults.standard.value(forKey: "URL") == nil
    {
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        let navController = UINavigationController(rootViewController: viewController)
        self.window?.rootViewController = navController
        self.window?.makeKeyAndVisible()

    }else{

        let viewController = storyboard.instantiateViewController(withIdentifier: "ChannelsViewController") as! ChannelsViewController
        let navController = UINavigationController(rootViewController: viewController)
        self.window?.rootViewController = navController
        self.window?.makeKeyAndVisible()
    }

    return true
}

最佳答案

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


let storyboard = UIStoryboard(name: "Main", bundle: nil)

    if UserDefaults .standard .value(forKey: "URL") == nil
    {
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()

    }else{

        let viewController = storyboard.instantiateViewController(withIdentifier: "ChannelsViewController") as! ChannelsViewController
        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()
    }


    return true
}

09-25 17:32