如何使用由 Xcode 编写的 TabBarController 管理的 ViewControllers 处理 iOS 应用程序的静态 QuickActions Swift
How to handle static QuickActions for iOS app using ViewControllers managed by a TabBarController in Xcode written in Swift
我已经在 Info.plist 中为我在 Xcode 中创建并在 Swift 中编写的 iOS 应用程序创建了一些(静态)QuickActions。
我无法让他们打开 ViewController。因为,我已经用谷歌搜索了,但对我没有任何帮助。如果这很重要:我正在使用由 TabBarController 管理的 ViewController。大多数教程似乎都使用 NavigationController。但是,我认为这将通过 segue 完成,对吗?我需要什么代码来处理它?
有人可以提供吗?或者有人知道一个简单的 manual/tutorial?
此致,大卫。
P.S.:
我试过这段代码,但它似乎只适用于 NavigationController?!
代码:
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void)
{
self.handleShortcutItem(shortcutItem)
completionHandler(true)
}
func handleShortcutItem(shortcutItem: UIApplicationShortcutItem)
{
switch shortcutItem.type {
case "icons.quickaction.home":
self.presentComposeViewController()
default: break
}
}
func presentComposeViewController()
{
guard let navigationController = window?.rootViewController as? UINavigationController else { return }
let identifier = "MyViewController"
let composeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(identifier)
navigationController.pushViewController(composeViewController, animated: false)
}
在@ILikeTau的帮助下,我终于找到了解决方案。
我正在使用以下代码打开由带有 QuickAction 的 TabBarController 管理的 ViewController:
@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
if(shortcutItem.type == "app.quickaction.search"){
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateInitialViewController()
window?.rootViewController = vc
guard let tabBarController = window?.rootViewController as? UITabBarController else { return };
tabBarController.selectedIndex = 2
}
else if(shortcutItem.type == "app.quickaction.home"){
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateInitialViewController()
window?.rootViewController = vc
guard let tabBarController = window?.rootViewController as? UITabBarController else { return };
tabBarController.selectedIndex = 0
}
}
此代码适用于两种模式:应用程序处于后台模式并且应用程序已关闭。我觉得这种方式比普通的多功能方式更简单更短。
我已经在 Info.plist 中为我在 Xcode 中创建并在 Swift 中编写的 iOS 应用程序创建了一些(静态)QuickActions。
我无法让他们打开 ViewController。因为,我已经用谷歌搜索了,但对我没有任何帮助。如果这很重要:我正在使用由 TabBarController 管理的 ViewController。大多数教程似乎都使用 NavigationController。但是,我认为这将通过 segue 完成,对吗?我需要什么代码来处理它?
有人可以提供吗?或者有人知道一个简单的 manual/tutorial?
此致,大卫。
P.S.: 我试过这段代码,但它似乎只适用于 NavigationController?! 代码:
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void)
{
self.handleShortcutItem(shortcutItem)
completionHandler(true)
}
func handleShortcutItem(shortcutItem: UIApplicationShortcutItem)
{
switch shortcutItem.type {
case "icons.quickaction.home":
self.presentComposeViewController()
default: break
}
}
func presentComposeViewController()
{
guard let navigationController = window?.rootViewController as? UINavigationController else { return }
let identifier = "MyViewController"
let composeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(identifier)
navigationController.pushViewController(composeViewController, animated: false)
}
在@ILikeTau的帮助下,我终于找到了解决方案。
我正在使用以下代码打开由带有 QuickAction 的 TabBarController 管理的 ViewController:
@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
if(shortcutItem.type == "app.quickaction.search"){
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateInitialViewController()
window?.rootViewController = vc
guard let tabBarController = window?.rootViewController as? UITabBarController else { return };
tabBarController.selectedIndex = 2
}
else if(shortcutItem.type == "app.quickaction.home"){
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateInitialViewController()
window?.rootViewController = vc
guard let tabBarController = window?.rootViewController as? UITabBarController else { return };
tabBarController.selectedIndex = 0
}
}
此代码适用于两种模式:应用程序处于后台模式并且应用程序已关闭。我觉得这种方式比普通的多功能方式更简单更短。