使用 TabBarController 呈现 ViewController

Presenting ViewController with TabBarController

好的,所以每次我尝试使用以下代码呈现 ViewController(来自 AppDelegate 文件的 "didFinishLaunchingWithOptions" 函数)时,我的带有 4 个项目的 tabBar 消失了,即使 ViewController我要的是:

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    let tabBarController = storyBoard.instantiateViewController(withIdentifier: "theEvents") as! ThirdViewController

    self.window?.rootViewController? = tabBarController

当我尝试以下代码(也在 AppDelegate 文件的 "didFinishLaunchingWithOptions" 函数中)时,我什么也没得到:

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    let tabBarController = storyBoard.instantiateViewController(withIdentifier: "theEvents") as! ThirdViewController

    self.window?.rootViewController?.tabBarController?.didMove(toParentViewController: tabBarController)

我已经尝试了多次迭代两者,但我似乎仍然无法得到我想要的...

本质上,我想要的是以编程方式按下其中一个按钮,以便我的第三个视图控制器成为用户在某些情况下看到的第一个视图。

如有任何帮助,我们将不胜感激!

不清楚 ThirdViewController 到底是什么,但我猜它是包含在选项卡控制器的第三个选项卡中的视图控制器。

标签栏控制器仍然需要是 window 的 rootViewController

如果你只是想以编程方式select标签栏控制器的某个索引,你可以这样做:

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let tabVC = storyboard.instantiateViewController(withIdentifier: "tabVC") as! UITabBarController  // however you defined your UITabBarController.
        tabVC.selectedIndex = 2   // or whatever you want
        window?.rootViewController = tabVC

如果tab bar controller已经是根目录,那么你可以绕过实例化它的步骤并将其设置为根目录:

    if let tabVC = window?.rootViewController as? UITabBarController {
        tabVC.selectedIndex = 2
    }

好的,这就是我为我的应用所做的, 我所做的是,我总是导航到 MainView 并调用 performSegue 进行导航。然后我 pushViewController

class AppDelegate: UIResponder, UIApplicationDelegate {

var shortcutItem: UIApplicationShortcutItem?
var mainViewController:MainViewController?
static var  handled = false

enum ShortcutIdentifier: String {
    case AddExpense
    case AddIncome
    case AddExpenseType
    case AddIncomeType

    init?(fullIdentifier: String) {
        guard let shortIdentifier = fullIdentifier.components(separatedBy: ".").last else {
            return nil
        }
        self.init(rawValue: shortIdentifier)
    }


    func getUIViewController(storyboard : UIStoryboard) -> UIViewController{
        return storyboard.instantiateViewController(withIdentifier:self.rawValue)
    }
}


func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    self.shortcutItem = shortcutItem
    completionHandler(handleShortcut(shortcutItem))
}


@discardableResult fileprivate func handleShortcut(_ shortcutItem: UIApplicationShortcutItem) -> Bool {

    let shortcutType = shortcutItem.type
    guard let shortcutIdentifier = ShortcutIdentifier(fullIdentifier: shortcutType) else {
        return false
    }

    return selectTabBarItemForIdentifier(shortcutIdentifier)
}

fileprivate func selectTabBarItemForIdentifier(_ identifier: ShortcutIdentifier) -> Bool {
    if AppDelegate.handled  {
        return false
    }

    var handled = false

    guard let tabBarController = self.window?.rootViewController as? UITabBarController else {
        return false
    }

    tabBarController.selectedIndex = 1

    if let contentViewController  = tabBarController.selectedViewController?.contentViewController as? MainViewController  {
        mainViewController = contentViewController
    }else{

        guard let  navigationController = tabBarController.selectedViewController?.contentViewController.navigationController else {
            return false
        }


        print(type(of:navigationController.contentViewController))
        print(navigationController.viewControllers.count)

        let _ = navigationController.popToRootViewController(animated: true)

    }

    guard let rootView = mainViewController else {
        return false
    }

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

    switch (identifier) {
    case .AddExpense:
        rootView.performSegue(withIdentifier: "Expences", sender: rootView)
        handled = true
    case .AddIncome:
        rootView.performSegue(withIdentifier: "Incomes", sender: rootView)
        handled = true
    case .AddExpenseType:
        rootView.performSegue(withIdentifier: "Expences Types", sender: rootView)
        handled = true
    case .AddIncomeType:
        rootView.performSegue(withIdentifier: "Incomes Types", sender: rootView)
        handled = true
    }

    rootView.navigationController!.pushViewController(identifier.getUIViewController(storyboard: storyboard), animated: true)
    AppDelegate.handled = handled
    return handled
}

var window: UIWindow?


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

    let theme = ThemManager.currentTheme()
    ThemManager.applyTheme(theme: theme)

    var performShortcutDelegate = true

    if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {

        print("Application launched via shortcut")
        self.shortcutItem = shortcutItem

        performShortcutDelegate = false
    }

    return performShortcutDelegate

}



func applicationDidEnterBackground(_ application: UIApplication) {

    AppDelegate.handled = false
}

func applicationDidBecomeActive(_ application: UIApplication) {

    print("Application did become active")

    guard let shortcut = shortcutItem else { return }

    print("- Shortcut property has been set")

    handleShortcut(shortcut)
    AppDelegate.handled = true
    self.shortcutItem = nil
}

}