![](/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.