Uiviewcontroller 加上「;“快速行动”;到我的iOS 9应用程序
我想将iOS 9的快速操作添加到我的应用程序中 我将此代码放在我的应用程序代理中:Uiviewcontroller 加上「;“快速行动”;到我的iOS 9应用程序,uiviewcontroller,swift2,ios9,Uiviewcontroller,Swift2,Ios9,我想将iOS 9的快速操作添加到我的应用程序中 我将此代码放在我的应用程序代理中: import UIKit enum ShortcutType: String { case NewScan = "QuickAction.NewScan" case Settings = "QuickAction.Settings" } @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { v
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
}
}
现在我可以在我的应用程序图标上进行强制触摸>将显示快速操作>我选择快速操作“新扫描”>应用程序将打开并显示我离开的最后一个视图
但该段将不会执行
以下是我故事板的一部分:
说明:
A:导航控制器和初始控制器
B:ViewController,检查后将转到导航控制器C
C:导航控制器
D:表视图控制器
视图控制器
如果我选择“使用快速操作进行新扫描”-我想显示ViewController E.,那么您似乎正在根据快速操作进行正确的操作。但是,在
handleShortCutItem:
实现中有很多可选链接。您是否使用了调试器来验证这些表达式都没有nil值?此外,从我所看到的(尽管图像模糊),故事板中第一个导航控制器的根视图控制器没有到E的序列。因此我不确定您打算如何到达那里
我建议您在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
}
func handleShortCutItem(shortcutItem:UIApplicationShortcutItem)->Bool{
guard let shortcutType=shortcutType.init(rawValue:shortcutItem.type)else{
返回错误
}
guard让rootNavigationController=window?.rootViewController作为?UINavigationController else{
返回错误
}
guard let rootViewController=rootNavigationController?.viewControllers.first-else{
返回错误
}
guard let storyboard=rootNavigationController.storyboard else{
返回错误
}
var viewControllers=[rootViewController]
开关快捷键类型{
案例.新闻扫描:
//为这种情况实例化必要的视图控制器
viewControllers+=[storyboard.InstanceViewControllerWithiIdentifier(“”)
...
viewControllers+=[storyboard.InstanceViewControllerWithiIdentifier(“”)
案例设置:
//为这种情况实例化必要的视图控制器
viewControllers+=[storyboard.InstanceViewControllerWithiIdentifier(“”)
...
viewControllers+=[storyboard.InstanceViewControllerWithiIdentifier(“”)
}
//设置新的视图控制器阵列
rootNavigationController.SetViewController(ViewController,动画:false)
返回真值
}
注意:由于您用Swift2标记了这个问题,我已经随意调整了代码以使用guard语句