如何使用由 Swift 编写的 Xcode 中的 TabBarController 管理的 ViewControllers 处理 iOS 应用程序的静态 QuickActions [英] How to handle static QuickActions for iOS app using ViewControllers managed by a TabBarController in Xcode written in Swift

查看:30
本文介绍了如何使用由 Swift 编写的 Xcode 中的 TabBarController 管理的 ViewControllers 处理 iOS 应用程序的静态 QuickActions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Info.plist 中为我在 Xcode 中创建并用 Swift 编写的 iOS 应用程序创建了一些(静态)QuickAction.

我在让他们能够打开 ViewController 时遇到问题.当然,我已经用谷歌搜索过了,但没有任何效果.如果这很重要:我正在使用由 TabBarController 管理的 ViewController.大多数教程似乎都使用 NavigationController.但是,我认为这将通过 segue 完成,对吗?我需要什么代码来处理它?<​​/p>

谁能提供一下?或者有人知道简单的手册/教程吗?

问候,大卫.

附:我试过这段代码,但它似乎只适用于 NavigationController?!代码:

func application(application: UIApplication, performActionForShortcutItemshortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void){self.handleShortcutItem(shortcutItem)完成处理程序(真)}func handleShortcutItem(shortcutItem: UIApplicationShortcutItem){切换快捷方式Item.type {案例icons.quickaction.home":self.presentComposeViewController()默认值:中断}}funcpresentComposeViewController(){守卫让 navigationController = window?.rootViewController as?UINavigationController else { return }让标识符 = "MyViewController"让 composeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(identifier)navigationController.pushViewController(composeViewController,动画:假)}

解决方案

我终于在@ILikeTau.

我使用以下代码打开由带有 QuickAction 的 TabBarController 管理的 ViewController:

@available(iOS 9.0, *)func应用程序(应用程序:UIApplication,performActionForShortcutItem快捷方式:UIApplicationShortcutItem,completionHandler:(Bool)-> Void){if(shortcutItem.type == "app.quickaction.search"){let sb = UIStoryboard(name: "Main", bundle: nil)让 vc = sb.instantiateInitialViewController()window?.rootViewController = vc守卫让 tabBarController = window?.rootViewController as?UITabBarController else { return };tabBarController.selectedIndex = 2}else if(shortcutItem.type == "app.quickaction.home"){let sb = UIStoryboard(name: "Main", bundle: nil)让 vc = sb.instantiateInitialViewController()window?.rootViewController = vc守卫让 tabBarController = window?.rootViewController as?UITabBarController else { return };tabBarController.selectedIndex = 0}}

此代码适用于两种模式:应用处于后台模式且应用已关闭.我认为这种方式比具有多种功能的普通方式更容易和更短.

I already created some (static) QuickActions in Info.plist for my iOS app created in Xcode and written in Swift.

I have problems with making them able to open a ViewController. Of cause, I already googled, but nothing worked for me. If this counts: I'm using ViewController managed by a TabBarController. Most tutorials seem to use NavigationController. But, I think it will be done with the segues, right? What code do I need to handle it?

Can anybody provide it please? Or does anybody know a simple manual/tutorial?

Regards, David.

P.S.: I tried this code, but it seems to work only with NavigationController?! Code:

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)
}

解决方案

I finally found the solution with the help from @ILikeTau.

I'm using the following code to open my ViewControllers managed by a TabBarController with QuickAction:

@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
    }
}

This code works from both modes: app is in background mode and app is closed. I think this way is easier and shorter than the common way with multiple functions.

这篇关于如何使用由 Swift 编写的 Xcode 中的 TabBarController 管理的 ViewControllers 处理 iOS 应用程序的静态 QuickActions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆