使用TabBarController呈现ViewController

时间:2017-03-31 02:02:36

标签: ios uiviewcontroller swift3 uitabbarcontroller xcode8

好的,所以每当我尝试使用以下代码呈现ViewController(来自AppDelegate文件" didFinishLaunchingWithOptions"函数)时,即使ViewController,我的tabBar也会消失4个项目我希望显示:

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

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

    self.window?.rootViewController? = tabBarController

当我尝试以下代码时(也在AppDelegate文件' s" 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)

我已经尝试了两次迭代,但我仍然无法得到我想要的东西......

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

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

目前还不清楚ThirdViewController到底是什么,但我猜测它是包含在标签控制器第三个标签中的视图控制器。

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

如果您只是想以编程方式选择标签栏控制器的某个索引,您可以执行以下操作:

        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

如果标签栏控制器已经是根,那么您可以绕过实例化它的步骤并将其设置为根目录:

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

答案 1 :(得分:0)

好的,这就是我为我的应用做的, 我做的是,我总是导航到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
}

}