简体   繁体   English

应用程序快捷方式打开TableView行

[英]Application Shortcut Open TableView Row

I have four application shortcuts set up when force pressing my app icon. 强制按下我的应用程序图标时,我设置了四个应用程序快捷方式。 I have a tab bar controller with navigation controllers each with table view controllers as the root view controller for the first two tabs. 我有一个带有导航控制器的标签栏控制器,每个控制器都带有表视图控制器,作为前两个标签的根视图控制器。

For the shortcuts, how can I open either the first or second tab and select a row from the corresponding table view? 对于快捷方式,如何打开第一个或第二个选项卡并从相应的表格视图中选择一行?

I think I would start with this but please correct me if I'm wrong. 我想我将从此开始,但是如果我错了,请纠正我。

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

---EDIT--- After working with the answer provided, I have this somewhat working. ---编辑---在使用提供的答案后,我做了一些工作。

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

This selects the correct row and opens the correct view controller. 这将选择正确的行并打开正确的视图控制器。 The issue I'm having with it now is that it is pushing the view controller for the selected row twice so the view controller that is pushed when selecting that row is being loaded twice. 我现在遇到的问题是它将两次为所选行推送视图控制器,因此在选择该行时被推送的视图控制器将被加载两次。

You need to receive the shortcut item in app delegate, then take appropriate action based on the type of shortcut that you will have defined in info.plist . 您需要在应用程序委托中接收快捷方式项,然后根据将在info.plist定义的快捷方式类型采取适当的措施。 Something like this should work, though it may need amending based on the exact structure of your app or your subclass names etc. 尽管可能需要根据您的应用程序或子类名称等的确切结构进行修改,但这样的事情应该可以工作。

In App Delegate: 在应用程序委托中:

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