UITabBarController无法在应用启动时为Quick Action for More选项卡设置选定的选项卡

时间:2017-03-14 01:26:12

标签: uinavigationcontroller uitabbarcontroller uiapplicationshortcutitem

我有一个使用带有7个标签的UITabBarController的应用。每个选项卡都是一个UIViewController子类(每个都嵌入在UINavigationController中),它在故事板中的视图集中只有不同的背景颜色。 TabItems标记为Tab 1到Tab 7,每个NavBar中的Title集合只是Tab号。我在Info.plist中添加了一些静态Quick Actions,允许我跳转到Tab 2,Tab 3,Tab 6和Tab 7。

我遇到的问题是当我在AppDelegate中处理快速操作时设置选定的Tab时,前4个选项卡的工作正常。如果我选择更多...列表中列出的其中一个选项卡,应用程序只会选择我的UITabBarController中的第一个选项卡。但是,如果应用程序已经运行并且我转到主屏幕并再次尝试快速操作,则可以从更多列表中选择任何选项卡。有什么想法吗?

这是我的AppDelegate代码:

//  AppDelegate.swift

import UIKit

@UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate 
{

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        // handle quick actions
        if let shortcutItem =
        launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem]
            as? UIApplicationShortcutItem {

            let _ = handleShortcut(shortcutItem: shortcutItem)
            return false
        }

        return true
    }

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

        completionHandler(handleShortcut(shortcutItem: shortcutItem))
    }

    private func handleShortcut(shortcutItem: UIApplicationShortcutItem) -> Bool {
        let shortcutType = shortcutItem.type
        guard let shortcutIdentifier = ShortcutIdentifier(fullIdentifier: shortcutType) else {
            return false
        }

        switch shortcutIdentifier {
        case ShortcutIdentifier.OpenTab2: fallthrough
        case ShortcutIdentifier.OpenTab3: fallthrough
        case ShortcutIdentifier.OpenTab6: fallthrough
        case ShortcutIdentifier.OpenTab7:
            return selectTabBarItem(forIdentifier: shortcutIdentifier)
        }
    }

    private func selectTabBarItem(forIdentifier identifier: ShortcutIdentifier) -> Bool {
        if let tabBarController = self.window?.rootViewController as? CustomTabBarController
        {

            switch (identifier)
            {
            case .OpenTab2:
                tabBarController.selectedIndex = tabDictionary["OpenTab2"]!
            case .OpenTab3:
                tabBarController.selectedIndex = tabDictionary["OpenTab3"]!
            case .OpenTab6:
                tabBarController.selectedIndex = tabDictionary["OpenTab6"]!
            case .OpenTab7:
                tabBarController.selectedIndex = tabDictionary["OpenTab7"]!
            }
        }
        return true
    }

    // Integer in dictionary denotes tab number (zero based)
    private let tabDictionary = ["OpenTab2": 1, "OpenTab3": 2, "OpenTab6": 5, "OpenTab7": 6]

    enum ShortcutIdentifier: String {
        case OpenTab2
        case OpenTab3
        case OpenTab6
        case OpenTab7

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

}

1 个答案:

答案 0 :(得分:0)

我发现以下链接(http://jakzaprogramowac.pl/pytanie/18417,select-index-greater-than-3-for-uitabbarcontroller-at-app-launch-not-working)似乎回答了我的问题。我希望这有助于其他人。

我将以下代码添加到AppDelegate DidFinishLaunchingWithOptions的开头,以便最初选择第一个选项卡并在tabBarController的视图中调用layoutIfNeeded():

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{
    // Override point for customization after application launch.

    // initially select first tab so more selection works from quick    actions - fixup
    if let tabBarController = self.window?.rootViewController as? CustomTabBarController
    {
        tabBarController.selectedIndex = 0
        tabBarController.view.layoutIfNeeded()
    }

    // handle quick actions
    if let shortcutItem =
    launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem]
        as? UIApplicationShortcutItem {

        let _ = handleShortcut(shortcutItem: shortcutItem)
        return false
    }

    return true
}