【问题标题】:Open specific UITabBarController / ViewController from SceneDelegate.swift从 SceneDelegate.swift 打开特定的 UITabBarController / ViewController
【发布时间】:2020-09-01 17:24:43
【问题描述】:

我有一个今天的扩展,它有一个UITableView

当用户点击 UITableView 中的某个项目时,它会使用 URL 方案打开包含应用程序并传递一些数据,以便包含应用程序可以打开用户点击的项目的特定视图。

问题是,从 SceneDelegate.swift 我可以处理 URL 方案,但似乎我无法显示特定的 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)

}

【问题讨论】:

  • 试试snoozedVC.tabBarController?.selectedIndex = todoStatusValueURL!
  • tabBar 包含SnoozedViewController?

标签: swift uiviewcontroller uitabbarcontroller today-extension uiscenedelegate


【解决方案1】:

首先检查该特定 snoozedVC 的 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")
             }
          }

【讨论】:

  • 非常感谢!通过这样做,我意识到SnoozedViewController 不存在,让我猜测真正的问题并且可以解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
相关资源
最近更新 更多