0

我有一个使用带有 7 个选项卡的 UITabBarController 的应用程序。每个选项卡都是一个 UIViewController 子类(每个都嵌入在 UINavigationController 中),它只是在情节提要中设置的视图中具有不同的背景颜色。TabItems 标记为 Tab 1 到 Tab 7,每个 NavBar 中设置的 Title 只是 Tab 编号。我在 Info.plist 中添加了一些静态快速操作,允许我跳转到选项卡 2、选项卡 3、选项卡 6 和选项卡 7。

我遇到的问题是,当我在 AppDelegate 中处理快速操作时设置选定的选项卡时,前 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)
        }
    }

}
4

1 回答 1

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
}        
于 2017-03-17T17:21:47.687 回答