从 SceneDelegate.swift 打开特定的 UITabBarController / ViewController
Open specific UITabBarController / ViewController from SceneDelegate.swift
我有一个 Today Extension,它有一个 UITableView
。
当用户点击 UITableView
中的一项时,它会使用 URL Scheme 打开包含的应用程序并传递一些数据,以便包含的应用程序可以打开特定的视图用户点击的项目。
问题是,从 SceneDelegate.swift 我可以处理 URL 方案,但似乎我无法进行特定的 UITabBarController
显示并打开所点击项目的详细视图由用户。
请注意,我并不是要创建它们;它们已经从情节提要中创建并开始工作。我只是希望它自动 'Navigate' 就像用户点击了 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 的 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")
}
}
我有一个 Today Extension,它有一个 UITableView
。
当用户点击 UITableView
中的一项时,它会使用 URL Scheme 打开包含的应用程序并传递一些数据,以便包含的应用程序可以打开特定的视图用户点击的项目。
问题是,从 SceneDelegate.swift 我可以处理 URL 方案,但似乎我无法进行特定的 UITabBarController
显示并打开所点击项目的详细视图由用户。
请注意,我并不是要创建它们;它们已经从情节提要中创建并开始工作。我只是希望它自动 'Navigate' 就像用户点击了 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 的 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")
}
}