![](/img/trans.png)
[英]How to identify the viewcontroller is clicked from More Tab or Separate tab in UITabBarController?
[英]UITabBarController unable to set selected Tab from Quick Action for More tabs on app startup
我有一个使用带有7个选项卡的UITabBarController的应用程序。 每个选项卡都是一个UIViewController子类(每个子类都嵌入到UINavigationController中),该子类在情节提要中设置的视图中仅具有不同的背景色。 TabItem标记为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)
}
}
}
我发现以下链接( 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.