gpt4 book ai didi

swift - 如何以编程方式创建标签栏项目的 Action

转载 作者:行者123 更新时间:2023-11-28 13:32:47 26 4
gpt4 key购买 nike

我用代码创建了一个标签栏,但我不知道当有人点击某个项目时如何捕捉,我该怎么做?

private func setNav()
{
tabBar = UITabBarController()
menuItemNavList = Array<UINavigationController>()
for i in (0..<menuItemModelList.count) {
menuItemNavList.append(createNavController(title: menuItemModelList[i].title ?? "", imageName: menuItemModelList[i].iconUrl ?? "p"))
}
tabBar.viewControllers = menuItemNavList
tabBar.tabBar.tintColor = .white
tabBar.tabBar.barTintColor = UIColor(red: 31/255, green: 192/255, blue: 241/255, alpha: 1.0)
self.view.addSubview(tabBar.view)
}

private func createNavController(title: String, imageName: String)-> UINavigationController
{
let viewController = UIViewController()
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.title = title
let url = URL(string: imageName)
let data = try? Data(contentsOf: url!)
navController.tabBarItem.image = data != nil ? UIImage(data: data!) : UIImage(named: "icono_home")
return navController
}

最佳答案

如果我没有弄错你的要求,这个委托(delegate)函数会做:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//If you wanna do something to an item with its index
let indexOfTab = tabBar.items?.index(of: item)
if indexOfTab == 0 {
// The user has tapped the first item
}
//Else you directly get the tapped item here
print(item)
}

确保你的 Controller 是标签栏的委托(delegate)(为了让这个委托(delegate)函数起作用)

关于swift - 如何以编程方式创建标签栏项目的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57098698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com