2017-07-07 6 views
0

私はタブバーコントローラアプリケーションを持っており、そのタブの1つにボタンに割り当てられたアクションを持つUI Collection View Controllerがあります。このボタンはその魔法を実行し、タブバーの表示を別のものに変更する必要があります。しかし、私はそれをタブコントローラーに直接参照することはできません。UICollectionViewセルからTab Bar Controllerを参照する方法

tabBarControllerは、コントローラに割り当てられたクラス名です。

tabBarController.selectedIndex = 3 

をしてもエラーが言う

tabBarController.goToIndex(3) 

tabBarControllerクラスのメソッドを直接作成::だから、私が試した「goToIndex」のインスタンスメンバーがタイプtabBarController

どれをに使用することはできませんイデア?

答えて

1

イムは、あなたが右のそれを参照することで何を意味するかを理解少し問題を抱えて、うまくいけば、これは役立ちます、ありがとうございます。 tabBarControllerはUITabBarControllerのサブクラスであると仮定すると:あなたのタブコントローラ(のUIViewController)の一

class MyTabBarController: UITabBarController { 

    /// ... 

    func goToIndex(index: Int) { 

    } 
} 

あなたがself.tabBarControllerとあなたのUITabBarControllerを参照することができます。 self.tabBarControllerはオプションであることに注意してください。あなたのタブのUIViewControllerはUINavigationControllerの内部のUIViewControllerがある場合

self.tabBarController?.selectedIndex = 3 

、あなたはこのようなあなたのタブバーを参照する必要があります。

self.navigationController?.tabBarController 

があなたのサブクラスに関数を呼び出すには、あなたがキャストする必要がありますタブバーコントローラをカスタムサブクラスに追加します。

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

コメントに基づいて更新:

あなたは、あなたがそれ(推奨されません)セル自体のいずれかのプロパティ作らない限り、あなたは、細胞内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 
    } 
} 
+0

Thanks Kuhncjは、私がtabBarControllerのUIViewController 'child'にいるときに動作します。 TabBarController - > UIViewControllerがナビゲーションコントローラに組み込まれています - > UiCollectionView - > ReUsableCell - > Button 問題は次のとおりです。まだタブインデックスを変更できませんボタンを押すと、ReUsable Cellクラス内に戻ります。私はプロトコルを介してこれを行うことができ、UIColelctionViewを直接管理するUIViewControllerの関数を呼び出すことができますが、ReUsableCellクラスを介して直接変更する方法があるかどうか疑問に思っていました。 – guarinex

+0

良い実践方法であるリユースセルで直接行う方法はありません。自分の目標達成に近づくのに役立ついくつかの追加のフィードバックで私の回答を更新しましたが、最終的には委任または目標のアクション – kuhncj

+0

につきました。スーパー感謝、kuhncj。私はそれが可能であると想像していて、私はそれを理解することができませんでした。あなたの解決策は道のりです。 – guarinex

関連する問題