簡體   English   中英

應用程序快捷方式打開TableView行

[英]Application Shortcut Open TableView Row

強制按下我的應用程序圖標時,我設置了四個應用程序快捷方式。 我有一個帶有導航控制器的標簽欄控制器,每個控制器都帶有表視圖控制器,作為前兩個標簽的根視圖控制器。

對於快捷方式,如何打開第一個或第二個選項卡並從相應的表格視圖中選擇一行?

我想我將從此開始,但是如果我錯了,請糾正我。

let tabNav:UINavigationController = tabBar.viewControllers?[0] as! UINavigationController

---編輯---在使用提供的答案后,我做了一些工作。

let navigationController = tabBar.viewControllers![0] as! UINavigationController
let tableViewController = navigationController.viewControllers.first as! FederationTableViewController
let indexPath = IndexPath(row: 0, section: 0)
tableViewController.tableView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
tableViewController.tableView.delegate?.tableView!((tableViewController.tableView)!, didSelectRowAt: indexPath)

tabBar.selectedIndex = 0

這將選擇正確的行並打開正確的視圖控制器。 我現在遇到的問題是它將兩次為所選行推送視圖控制器,因此在選擇該行時被推送的視圖控制器將被加載兩次。

您需要在應用程序委托中接收快捷方式項,然后根據將在info.plist定義的快捷方式類型采取適當的措施。 盡管可能需要根據您的應用程序或子類名稱等的確切結構進行修改,但這樣的事情應該可以工作。

在應用程序委托中:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    print("Opening app from 3D touch shortcut...")
    completionHandler(handleShortcut(shortcutItem: shortcutItem))
}

// This function handles the shortcut. Any problems will return false, preventing the shortcut opening.
private func handleShortcut(shortcutItem: UIApplicationShortcutItem) -> Bool {
    if shortcutItem.type == "firstShortcutType" {
        guard let tabBarController = self.window.rootViewController as? UITabBarController else { return false }
        guard let navigationController = tabBarController.viewcontrollers[0] as? UINavigationController else { return false }
        guard let tableViewController = navigationController.rootViewController as? UITableViewController else { return false }

        // Select index of tab bar controller
        tabBarController.selectedIndex = 0

        // TableView May not be loaded, so I wrap this in a delayed block
        DispatchQueue.main.asyncAfter(deadline: .now()+1, execute: {
            // Create index path
            let indexPath = IndexPath(row: 0, section: 0)
            self.tableViewController.tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
        })
        return true

    } else if shortcutItem.type == "otherShortcutType" {
        // Handle ofher shortcut types
    } else {
        return false
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM