【发布时间】:2018-01-14 23:05:03
【问题描述】:
我的基于 Storyboard 的应用具有以下结构:
NavigationController (NavCa) —> CollectionViewController (VCa) —> 模态呈现 —> TabBarController (TabBarC)
TabBarC 有两个选项卡,每个选项卡都嵌入在自己的 NavigationController 中:
TabBar[0] NavigationController (NavCTab0) —> TableViewController (VC01) —> ViewController (VC02) —> ViewController (VC03)
TabBar[1] NavigationController (NavCTab1) —> ViewController (VC11) —> ViewController (VC12)
我的问题:我的一个 3D QuickAction 必须直接导航到 VC01。
我确实检查了该网站。主题Swift: Programmatically transitioning from 1 view to another via a TabBar and Navigation Controller 看起来非常接近我的。我尝试了代码(AppDelegate.swift 中的快速操作处理程序):
let mainSB = UIStoryboard(name: "Main", bundle: nil)
let tbc = mainSB.instantiateViewController(withIdentifier: “TabBarC”) as? TabBarController
tbc?.selectedIndex = 0
let ncTab0 = tbc?.viewControllers![0] as! UINavigationController
let vc01 = mainSB.instantiateViewController(withIdentifier: “VC01”) as! TableViewController01
let vc02 = mainSB.instantiateViewController(withIdentifier: “VC02”) as! ViewController02
let vc03 = mainSB.instantiateViewController(withIdentifier: “VC03”) as! ViewController03
let arrayOfVC = [vc01, vc02, vc03]
ncTab0.setViewControllers(arrayOfVC, animated: false)
ncTab0.popToViewController(vc01, animated: true)
self.window?.rootViewController?.present(tbc, animated: true, completion: nil)
但什么也没发生。输出窗口中没有任何内容。应用中什么也没有发生(如果应用被终止,它会启动 VCa 屏幕;如果应用没有被终止,它会停留在快速操作之前的位置)。
非常感谢任何帮助。
附:我只会说 SWIFT。
编辑
我的 AppDelegate.swift 和 Quick Action Handler:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
var isLaunchedFromQuickAction = false
// Check if it's launched from Quick Action
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
isLaunchedFromQuickAction = true
_ = handleQuickAction(shortcutItem)
}
// Return false if the app was launched from a shortcut, so performAction... will not be called.
return !isLaunchedFromQuickAction
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
completionHandler(handleQuickAction(shortcutItem))
}
func handleQuickAction(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
var quickActionHandled = false
let type = shortcutItem.type.components(separatedBy: ".").last!
if type == "QuickAction1" {
... // Navigating to VC01, see piece of code above
quickActionHandled = true
}
}
return quickActionHandled
}
我在其他应用中使用的相同快速操作处理程序。该应用程序的结构为 NavigationController --> ViewController1 --> ViewController2 --> ViewController3。还有一个快速操作导航到 ViewController2。它就像一个魅力。 与新应用程序的唯一区别是应用程序结构。我无法通过所有这些 TabBarControllers、NavigationControllers 等赢得导航......
编辑 2
按照建议,我尝试了 Apple 示例代码 (https://developer.apple.com/library/content/samplecode/ApplicationShortcuts/Introduction/Intro.html#//apple_ref/doc/uid/TP40016545-Intro-DontLinkElementID_2) 中的另一种快速操作处理方法。而且......它现在可以工作了。部分。
如果我终止应用程序,3D 快速操作将完美运行。
但是。如果我不终止应用程序并且仅在后台发送它 - 快速操作不起作用并且在控制台中我收到相同的旧消息:'警告:尝试在
有什么想法吗?
编辑 3
最终解决方案。
完全按照 Apple 示例代码 (https://developer.apple.com/library/content/samplecode/ApplicationShortcuts/Introduction/Intro.html#//apple_ref/doc/uid/TP40016545-Intro-DontLinkElementID_2) 中的 3D 快速操作在 AppDelegate.swift 中的开关 shortCutType 下进行快速操作并导航到“VC01”
case ShortcutIdentifier.first.type:
// Handle shortcut 1 (static).
let mainSB = UIStoryboard(name: "Main", bundle: nil)
let tbc = mainSB.instantiateViewController(withIdentifier: "TabBarC") as? TabBarController
self.window?.rootViewController?.dismiss(animated: true, completion: nil)
self.window?.rootViewController?.present(tbc!, animated: true, completion: nil)
handled = true
break
此代码经过测试并且有效。
【问题讨论】:
-
如果你有一个普通的基于 StoryBoard 的应用程序,你不需要实例化你自己的视图控制器。
-
@ryantxr 请继续。我很高兴看到您的解决方案。
-
您是否已通过调试器验证此代码是否正在运行?查看更多 AppDelegate 可能会有所帮助。您是否将 AppDelegate 代码与 ApplicationShortcuts 示例 Swift 应用程序进行了比较?
-
你的导航控制器有 segues 吗?
-
@ozzieozumo 代码正在运行。我的其他 3D 快速操作没有问题。我会说这是一个“标准”的 3D 快速动作实现。问题是导航...
标签: ios swift uitabbarcontroller programmatically