如何从 UICollectionView Cell 引用 Tab Bar Controller [英] how to reference the Tab Bar Controller from a UICollectionView Cell
问题描述
我有一个选项卡栏控制器应用程序,在其中一个选项卡中有一个 UI 集合视图控制器,其操作分配给一个按钮.这个按钮发挥了它的魔力,然后应该将标签栏视图更改为另一个.但是,我无法将其直接引用到选项卡控制器.
I have a tab bar controller application and in one of the tabs a UI Collection View Controller with an action assigned to a button. This button does its magic and then should change the tab bar view to another one. However, I'm not able to reference it right to the tab controller.
tabBarController 是分配给控制器的类名.所以,我试过:
tabBarController is the class name assigned to the controller. So, I tried:
tabBarController.selectedIndex = 3
并直接在tabBarController类中创建一个方法
and also creating a method directly in the tabBarController class
tabBarController.goToIndex(3)
错误提示:goToIndex"的实例成员不能用于类型 tabBarController
The error says: Instance member of 'goToIndex' cannot be used on type tabBarController
有什么想法吗?
谢谢,
推荐答案
我无法理解正确引用它的意思,但希望这会有所帮助.假设 tabBarController 是 UITabBarController 的子类:
Im having a little trouble understanding what you mean by referencing it right, but hopefully this will help. Assuming tabBarController is a subclass of UITabBarController:
class MyTabBarController: UITabBarController {
/// ...
func goToIndex(index: Int) {
}
}
在您的一个选项卡控制器 (UIViewController) 中,您可以使用 self.tabBarController
引用您的 UITabBarController.请注意 self.tabBarController 是可选的.
In one of your tab controllers (UIViewController) you can reference your UITabBarController with self.tabBarController
. Note that self.tabBarController is optional.
self.tabBarController?.selectedIndex = 3
如果您的选项卡 UIViewController 是 UINavigationController 内的 UIViewController,那么您需要像这样引用您的选项卡栏:
If your tab UIViewController is a UIViewController inside a UINavigationController, then you will need to reference your tab bar like this:
self.navigationController?.tabBarController
要在您的子类上调用函数,您需要将标签栏控制器强制转换为您的自定义子类.
To call a function on your subclass you would need to cast the tab bar controller to your custom subclass.
if let myTabBarController = self.tabBarController as? MyTabBarController {
myTabBarController.goToIndex(3)
}
<小时>
根据评论更新:
Update based on comments:
您无法访问单元格内的 tabBarController 是正确的,除非您将其设置为单元格本身(不推荐)或应用程序委托的属性.或者,您可以在 UIViewController 上使用目标操作,每次在单元格内点击按钮时调用视图控制器上的函数.
You're correct that you cant access the tabBarController inside the cell unless you made it a property on either the cell itself (not recommended) or the app delegate. Alternatively, you could use target action on your UIViewController to call a function on the view controller every time a button is tapped inside a cell.
class CustomCell: UITableViewCell {
@IBOutlet weak var myButton: UIButton!
}
class MyTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReuseIdentifier", for: indexPath) as! CustomCell
/// Add the indexpath or other data as a tag that we
/// might need later on.
cell.myButton.tag = indexPath.row
/// Add A Target so that we can call `changeIndex(sender:)` every time a user tapps on the
/// button inside a cell.
cell.myButton.addTarget(self,
action: #selector(MyTableViewController.changeIndex(sender:)),
for: .touchUpInside)
return cell
}
/// This will be called every time `myButton` is tapped on any tableViewCell. If you need
/// to know which cell was tapped, it was passed in via the tag property.
///
/// - Parameter sender: UIButton on a UITableViewCell subclass.
func changeIndex(sender: UIButton) {
/// now tag is the indexpath row if you need it.
let tag = sender.tag
self.tabBarController?.selectedIndex = 3
}
}
这篇关于如何从 UICollectionView Cell 引用 Tab Bar Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!