从SceneDelegate.swift打开特定的UITabBarController ViewController。

问题描述 投票:0回答:1

我有一个 "今日扩展",它有一个 UITableView.

当用户点击其中的一个项目时,就会发现,该项目是一个很好的例子。UITableView它用URL Scheme打开包含的应用程序,并传递一些数据,这样包含的应用程序就可以为用户点击的项目打开一个特定的视图。

问题是,从SceneDelegate.swift中,我可以处理URL Scheme,但似乎我不能为一个特定的 UITabBarController 显示并打开用户点击的项目的详细视图。

请注意,我并没有试图创建它们;它们已经从故事板中创建并工作了。我只是想让它自动 "导航",就像用户点击了tableview项目一样。

下面的代码是我试过的。

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    // This part, I'm handling URL Scheme which is working fine
    let url = URLContexts.first!.url
    let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
    let items = urlComponents?.queryItems
    var deepLinkArray: [URLQueryItem] = []
    deepLinkArray = items!


    // From here I will use datas I got from URL
    if let statusValue = deepLinkArray[0].value, let indexPathSection = deepLinkArray[1].value, let indexPathRow = deepLinkArray[2].value {
        todoStatusValueURL = Int(statusValue)!
        indexPathSectionURL = Int(indexPathSection)!
        indexPathRowURL = Int(indexPathRow)!



        // Here I want to show one of UITabBarController
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let snoozedVC = storyboard.instantiateViewController(withIdentifier: "SnoozedViewController") as! SnoozedViewController
        snoozedVC.self.tabBarController?.selectedIndex = todoStatusValueURL! // And this is not showing what I want


        // Additionally I want to show ItemDetailViewController as if user tapped the item in the tableview, and below code is also not working
        let detailVC = storyboard.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
        snoozedVC.navigationController?.pushViewController(detailVC, animated: true)

}
swift uiviewcontroller uitabbarcontroller today-extension uiscenedelegate
1个回答
0
投票

首先检查snoozedVC和TabBarController是否存在......它会让你知道你的代码有什么问题......然后根据它的结果前进,检查你的代码的哪个部分没有工作。

if let snoozedVC = storyboard.instantiateViewController(withIdentifier: "SnoozedViewController") as? SnoozedViewController {
    if let tabBarController = snoozedVC.tabBarController {

            tabBarController.selectedIndex = todoStatusValueURL!

            }  else {
                 print("tab controller not found")
             }
           } else {
                 print("SnoozedViewController not found")
             }
          }
© www.soinside.com 2019 - 2024. All rights reserved.