gpt4 book ai didi

ios - 在 SWIFT 2.x 中以编程方式调用带有 NavigationController 的 UIViewController

转载 作者:行者123 更新时间:2023-11-29 01:19:52 24 4
gpt4 key购买 nike

当我选择动态快捷方式时,我想以编程方式调用在 Storyboard 中设计的具有 NavigationController 的 UIViewController。

我在 Main.storyboard 中设计了 UIViewController 及其 NavigationController,并放置了一个 Storyboard 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 触摸选择我的快捷方式时,我的第二个 View Controller 将被调用并且它的 UIView 将被显示..但没有它的导航 Controller 。否则,如果我通过 Storyboard中设计的按钮调用我的 secondUIcontroller(具有相关的 segue 回调),secondUIView 将正确显示导航 Controller ..

怎么了?

最佳答案

您需要将您的第一个 View Controller 嵌入到 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 中,您需要推送第二个 View Controller 而不是当前 View Controller 。所以你的代码应该是:

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)

// }
}

关于ios - 在 SWIFT 2.x 中以编程方式调用带有 NavigationController 的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34746605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com