当我选择动态快捷方式时,我想以编程方式调用带有在Storyboard中设计的NavigationController的UIViewController。
我在Main.storyboard中设计了我的UIViewController及其NavigationController,我输入了一个故事板ID(我称之为MyUICtrlStoryID)
为了创建我的动态快捷方式,我在AppDelegate中编写了以下代码:
@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
let handledShortCutItem = shortcutItem.type // handleShortCutItem(shortcutItem)
if handledShortCutItem == "3DTouchShourtcutID"{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
//FirstViewcontroller UI will be called as root UIView
let initialViewControlleripad : FirstViewController = storyboard.instantiateViewControllerWithIdentifier("FirstViewControllerID") as! FirstViewController
initialViewControlleripad.go2MySecondUI = true //set this check variable in order to launch my second UI View from the first one.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
}
在我的FistViewController的viewWillAppear函数中
if #available(iOS 9.0, *) {
let shortcutItem = UIApplicationShortcutItem(type: "3DTouchShortcutID",
localizedTitle: "This action",
localizedSubtitle: "action description",
icon: UIApplicationShortcutIcon(type: UIApplicationShortcutIconType.Add),
userInfo: nil)
UIApplication.sharedApplication().shortcutItems = [shortcutItem]
//check if FirstViewControler had to call the second UI
if go2MySecondUI == true {
self.go2MySecondUI = false
let newViewCtrl = self.storyboard?.instantiateViewControllerWithIdentifier("MyUICtrlStoryID") as? SecondViewController
// call second UI view
dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(newViewCtrl!, animated: true, completion: nil)
}
// }
}
此代码工作正常:当我通过3d touch选择我的快捷方式时,我的secondviewcontroller将被调用并且它的UIView将被显示..但是没有它的导航控制器。 否则,如果我通过故事板中设计的按钮调用我的secondUIcontroller(带有相关的segue回调),则第二个UIView将正确显示导航控制器..
有什么问题?
答案 0 :(得分:1)
您需要在UINavigationController中嵌入第一个视图控制器。所以在AppDelegate中你的代码应该是:
@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
let handledShortCutItem = shortcutItem.type // handleShortCutItem(shortcutItem)
if handledShortCutItem == "3DTouchShourtcutID"{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
//FirstViewcontroller UI will be called as root UIView
let initialViewControlleripad : FirstViewController = storyboard.instantiateViewControllerWithIdentifier("FirstViewControllerID") as! FirstViewController
initialViewControlleripad.go2MySecondUI = true //set this check variable in order to launch my second UI View from the first one.
let navigationController = UINavigationController(rootViewController: initialViewControlleripad)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
}
然后在FirstViewController的viewWillAppear中,您需要推送第二个视图控制器而不是存在。所以你的代码应该是:
if #available(iOS 9.0, *) {
let shortcutItem = UIApplicationShortcutItem(type: "3DTouchShortcutID",
localizedTitle: "This action",
localizedSubtitle: "action description",
icon: UIApplicationShortcutIcon(type: UIApplicationShortcutIconType.Add),
userInfo: nil)
UIApplication.sharedApplication().shortcutItems = [shortcutItem]
//check if FirstViewControler had to call the second UI
if go2MySecondUI == true {
self.go2MySecondUI = false
let newViewCtrl = self.storyboard?.instantiateViewControllerWithIdentifier("MyUICtrlStoryID") as? SecondViewController
// call second UI view
self.navigationController?.pushViewController(newViewCtrl, animated: true)
// }
}