【问题标题】:UITabBarController unable to set selected Tab from Quick Action for More tabs on app startupUITabBarController 无法在应用启动时为更多选项卡的快速操作设置选定选项卡
【发布时间】:2017-08-04 05:10:51
【问题描述】:

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

}

【问题讨论】:

    标签: uinavigationcontroller uitabbarcontroller uiapplicationshortcutitem


    【解决方案1】:

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

    我在 AppDelegate DidFinishLaunchingWithOptions 的开头添加了以下代码,以初始选择第一个选项卡并在 tabBarController 的 View 上调用 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
    }        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2023-04-10
      相关资源
      最近更新 更多