【问题标题】:Add "quick actions" to my iOS 9 app向我的 iOS 9 应用程序添加“快速操作”
【发布时间】:2015-09-28 10:16:45
【问题描述】:

我想将 iOS 9 的快速操作添加到我的应用程序中。

我将此代码放入我的应用委托中:

import UIKit
enum ShortcutType: String {
    case NewScan = "QuickAction.NewScan"
    case Settings = "QuickAction.Settings"
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    static let applicationShortcutUserInfoIconKey = "applicationShortcutUserInfoIconKey"

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        UIViewController.prepareInterstitialAds()

        if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) {
            UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
        }

        // QUICK ACTIONS
            var launchedFromShortCut = false

            if #available(iOS 9.0, *) {
                if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
                    launchedFromShortCut = true
                    handleShortCutItem(shortcutItem)
                }
            } else {
                return true
            }
            return !launchedFromShortCut

    }

    /**************** QUICK ACTIONS ****************/
    @available(iOS 9.0, *)
    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
            let handledShortCutItem = handleShortCutItem(shortcutItem)
            completionHandler(handledShortCutItem)
    }
    @available(iOS 9.0, *)
    func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
        var handled = false
        if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) {
            let rootNavigationViewController = window!.rootViewController as? UINavigationController
            let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?
            rootNavigationViewController?.popToRootViewControllerAnimated(false)
            switch shortcutType {
                case .NewScan:

                    rootViewController?.performSegueWithIdentifier("goToCamera", sender: nil)
                    handled = true

                case.Settings:
                    rootViewController?.performSegueWithIdentifier("goToSettings", sender: nil)
                    handled = true
            }
        }
        return handled
    }
}

现在我可以用力触摸我的应用程序图标 > 将显示快速操作 > 我选择快速操作“新扫描” > 应用程序将打开并显示我离开的最后一个视图。

但是segue不会被执行。

这是我的故事板的一部分:

解释:

A:导航控制器和初始化控制器

B:ViewController,检查后会跳转到导航控制器C

C:导航控制器

D:表视图控制器

E: 视图控制器

如果我选择带有快速操作的新扫描 - 我想显示 ViewController E。

【问题讨论】:

    标签: uiviewcontroller swift2 ios9


    【解决方案1】:

    您似乎根据example code in the documentation 正确地做事。但是,您的handleShortCutItem: 实现中有很多可选链接。您是否使用调试器来验证这些表达式都没有 nil 值?此外,据我所知(尽管图像模糊),该故事板中第一个导航控制器的根视图控制器与 E 没有segue。所以我不确定你打算如何到达那里。

    我建议您在 handleShortCutItem: 实现中设置一个断点,以首先验证您正在使用的值不是 nil 并且代码实际上正在执行。完成此操作后,您可以使用情节提要来实例化您想要的视图控件并创建一个数组,因为您希望视图控制器层次结构位于导航控制器中,并将导航控制器的 viewControllers 属性设置为此大批。同样,很难准确地说出您想要从图像中得到什么,但可能是这样的:

    func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
        guard let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) else {
            return false
        }
    
        guard let rootNavigationController = window?.rootViewController as? UINavigationController else {
            return false
        }
    
        guard let rootViewController = rootNavigationController?.viewControllers.first else {
            return false
        }
    
        guard let storyboard = rootNavigationController.storyboard else {
            return false
        }
    
        var viewControllers = [rootViewController]
        switch shortcutType {
        case .NewScan:
            // Instantiate the necessary view controllers for this case
            viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some view controller#>")]
            ...
            viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some other view controller#>")]
    
        case.Settings:
            // Instantiate the necessary view controllers for this case
            viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some view controller#>")]
            ...
            viewControllers += [storyboard.instantiateViewControllerWithIdentifier("<#Identifier for some other view controller#>")]
        }
    
        // Set the new view controllers array
        rootNavigationController.setViewControllers(viewControllers, animated: false)
    
        return true
    }
    

    注意:由于您使用 Swift2 标记了此问题,因此我冒昧地调整代码以使用保护语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2013-07-28
      相关资源
      最近更新 更多