我有一个UITabBarController的子类,声明如下
class TabbarViewController: UITabBarController, UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
self.currentTabIndex = tabBarController.selectedIndex
if self.currentTabIndex == 2 {
let reportVC = UIWindow.getVisibleViewControllerFrom(tabBarController) as? ReportsViewController
if let reportsViewController = reportVC {
if reportsViewController.reportTableView.numberOfRows(inSection: 0) > 0 {
reportVC?.reportTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
let navVC = viewController as? UINavigationController
let destinationVC = navVC?.viewControllers.last as? ReportsViewController
return (destinationVC != nil) ? false : true
}
}
}
return true
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let newIndex = tabBarController.selectedIndex
let tabIndexList = [0, 1]
if self.currentTabIndex == newIndex && tabIndexList.contains(newIndex) {
let hbgViewController = (newIndex == 2)
? tabBarController.selectedViewController as! UINavigationController
: tabBarController.selectedViewController as! DLHamburguerNavigationController
switch newIndex {
case 0:
let alertsViewController = hbgViewController.viewControllers[0] as! AlertsViewController
alertsViewController.alertsTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
break
case 1:
let newsViewController = hbgViewController.viewControllers[0] as! NewsViewController
newsViewController.newsTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
break
default:
break
}
}
}
}
当用户点击当前选定的标签时,桌面视图会滚动到顶部。这可以按预期工作,但是,当用户点击表视图中的项目时,webview将加载远程新闻文章。视图被解除后,上面的代码不再有效,
导航标签有效,但didSelect
和shouldSelect
不再被调用。我不明白为什么。
以下是webview的呈现方式:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newImageSource = self.newsArticles?[indexPath.row].getImageUrl(type: Article.FireNewsImageType.NewsFeed)
let articleURLString = self.newsArticles?[indexPath.row].url
let webViewController = GDWebViewController()
webViewController.title = self.newsArticles?[indexPath.row].title
let webVCNav = UINavigationController(rootViewController: webViewController)
webViewController.showsToolbar = false
webViewController.progressIndicatorStyle = .both
webViewController.loadURLWithString(articleURLString!)
let cancel = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(NewsViewController.dismissVC))
webViewController.navigationItem.rightBarButtonItem = cancel
self.present(webVCNav, animated: true, completion: nil)
}
如果需要,可以在此处找到GDWebViewController: https://gist.github.com/elimence/91cf35dfe677b38b23a27c6d54d44285
任何指针都将不胜感激,谢谢:)
答案 0 :(得分:2)
您似乎没有将UITabBarController
的{{1}}设置为delegate
班级
您可以将其放在TabbarViewController
或代码的其他部分,如下所示。
viewDidLoad()