how to reference the Tab Bar Controller from a UICollectionView Cell

两盒软妹~` 提交于 2019-12-12 03:52:20

问题


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 is the class name assigned to the controller. So, I tried:

tabBarController.selectedIndex = 3

and also creating a method directly in the tabBarController class

tabBarController.goToIndex(3)

The error says: Instance member of 'goToIndex' cannot be used on type tabBarController

Any ideia?

Thank you,


回答1:


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) {

    }
}

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

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:

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
    }
}


来源:https://stackoverflow.com/questions/44975025/how-to-reference-the-tab-bar-controller-from-a-uicollectionview-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!