如何从 UICollectionView 单元中引用选项卡栏控制器

Posted

技术标签:

【中文标题】如何从 UICollectionView 单元中引用选项卡栏控制器【英文标题】:how to reference the Tab Bar Controller from a UICollectionView Cell 【发布时间】:2017-07-07 15:51:37 【问题描述】:

我有一个选项卡栏控制器应用程序,其中一个选项卡中有一个 UI 集合视图控制器,该控制器具有分配给按钮的操作。这个按钮发挥了它的魔力,然后应该将标签栏视图更改为另一个。但是,我无法将它直接引用到选项卡控制器。

tabBarController 是分配给控制器的类名。所以,我尝试了:

tabBarController.selectedIndex = 3

并且还直接在 tabBarController 类中创建方法

tabBarController.goToIndex(3)

错误提示:'goToIndex' 的实例成员不能在类型 tabBarController 上使用

有什么想法吗?

谢谢,

【问题讨论】:

【参考方案1】:

通过正确引用它来理解您的意思有点困难,但希望这会有所帮助。假设 tabBarController 是 UITabBarController 的子类:

class MyTabBarController: UITabBarController 

    /// ...

    func goToIndex(index: Int) 

    

在您的选项卡控制器之一 (UIViewController) 中,您可以使用 self.tabBarController 引用您的 UITabBarController。注意 self.tabBarController 是可选的。

    self.tabBarController?.selectedIndex = 3

如果您的标签 UIViewController 是 UINavigationController 内的 UIViewController,那么您将需要像这样引用您的标签栏:

self.navigationController?.tabBarController

要在您的子类上调用函数,您需要将标签栏控制器强制转换为您的自定义子类。

    if let myTabBarController = self.tabBarController as? MyTabBarController 
        myTabBarController.goToIndex(3)
    

基于 cmets 的更新:

您是正确的,除非您将其设置为单元格本身(不推荐)或应用程序委托的属性,否则您无法访问单元格内的 tabBarController。或者,您可以使用 UIViewController 上的目标操作来在每次点击单元格内的按钮时调用视图控制器上的函数。

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
    

【讨论】:

感谢 Kuhncj,当我在 tabBarController 的 UIViewController 'child' 中时,它可以工作。试图更好地解释我的疑问,这是我的结构: TabBarController -> UIViewController embed in Navigation Controller -> UiCollectionView -> ReUsableCell -> Button 问题是:我仍然无法从 ReUsable Cell 内部更改选项卡索引按下按钮时上课。我知道我可以通过协议来做到这一点,并在 UIViewController 中调用一个函数,直接管理 UIColelctionView 但我想知道是否有办法通过 ReUsableCell 类直接更改它。 知道了。非常感谢,kuhncj。我在想象这是可能的,但我无法弄清楚。您的解决方案就是要走的路。

以上是关于如何从 UICollectionView 单元中引用选项卡栏控制器的主要内容,如果未能解决你的问题,请参考以下文章