T. *_*Rex 2 uitabbarcontroller ios swift
我有一个 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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当用户点击当前选择的选项卡时,tableview 会滚动到顶部。这按预期工作,但是,当用户点击 table view 中的项目时,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)
}
Run Code Online (Sandbox Code Playgroud)
如果需要,可以在此处找到 GDWebViewController:https ://gist.github.com/elimence/91cf35dfe677b38b23a27c6d54d44285
任何指针将不胜感激谢谢:)
似乎您没有将UITabBarController's设置delegate为您的TabbarViewController班级
您可以viewDidLoad()像这样将它放在代码的其他部分或其他部分。
override func viewDidLoad() {
super.viewDidLoad()
// this code right here.
self.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1208 次 |
| 最近记录: |