应用程序快捷方式打开 TableView 行
Application Shortcut Open TableView Row
当我用力按下我的应用程序图标时,我设置了四个应用程序快捷方式。我有一个带有导航控制器的选项卡栏控制器,每个导航控制器都带有 table 视图控制器作为前两个选项卡的根视图控制器。
对于快捷方式,如何从相应的 table 视图中打开第一个或第二个选项卡以及 select 一行?
我想我会从这里开始,但如果我错了请纠正我。
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
这 select 是正确的行并打开了正确的视图控制器。我现在遇到的问题是它正在为 selected 行推送视图控制器两次,因此在 selecting 该行时推送的视图控制器被加载两次。
您需要在 app delegate 中接收快捷方式项,然后根据您将在 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
}
}
当我用力按下我的应用程序图标时,我设置了四个应用程序快捷方式。我有一个带有导航控制器的选项卡栏控制器,每个导航控制器都带有 table 视图控制器作为前两个选项卡的根视图控制器。
对于快捷方式,如何从相应的 table 视图中打开第一个或第二个选项卡以及 select 一行?
我想我会从这里开始,但如果我错了请纠正我。
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
这 select 是正确的行并打开了正确的视图控制器。我现在遇到的问题是它正在为 selected 行推送视图控制器两次,因此在 selecting 该行时推送的视图控制器被加载两次。
您需要在 app delegate 中接收快捷方式项,然后根据您将在 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
}
}