Swift3 如何在更多导航控制器中获取项目的选定索引
我使用下面的代码获取选项卡栏控制器的选定项。我的UITabbar有7个视图控制器(更多选项卡中有3个项目)。 此代码仅适用于5个选项卡,但不会返回“更多”选项卡上项目的选定索引Swift3 如何在更多导航控制器中获取项目的选定索引,swift3,xcode8,Swift3,Xcode8,我使用下面的代码获取选项卡栏控制器的选定项。我的UITabbar有7个视图控制器(更多选项卡中有3个项目)。 此代码仅适用于5个选项卡,但不会返回“更多”选项卡上项目的选定索引 import UIKit class CustomTabbarController: UITabBarController{ override func viewDidLoad() { super.viewDidLoad() self.delegate = self } override
import UIKit
class CustomTabbarController: UITabBarController{
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print(self.selectedIndex)
}
}
按如下方式获取所选项目:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print(tabBar.items?.index(of: item))
}
下面的代码对我有用。我不得不重新设计moreTableView以遵循我的应用程序设计。函数'didSelectRowAt'返回所选的索引 此代码已添加到“UITabBarController”类中
var moreTableView: UITableView?
weak var currentTableViewDelegate: UITableViewDelegate?
func customizeMoreTableView() {
moreTableView = moreNavigationController.topViewController?.view as? UITableView
currentTableViewDelegate = moreTableView?.delegate
moreTableView?.delegate = self
moreTableView?.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (viewControllers?.count ?? 4) - 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let moreCell = UITableViewCell()
let item = viewControllers?[indexPath.row + 4].tabBarItem
moreCell.textLabel?.text = item?.title
moreCell.textLabel?.textColor = .white
moreCell.imageView?.image = item?.image
moreCell.imageView?.tintColor = .white
moreCell.backgroundColor = .black
return moreCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
currentTableViewDelegate?.tableView!(tableView, didSelectRowAt: indexPath)
}
你成功了吗?我也有同样的问题