Pr0tonion
Pr0tonion

Reputation: 207

I cannot get the tabbar delegate to call

I have asked this problem b4 and have tried for so long...

Problem: I have 4 tabs, when the third one is selected i want to wait until the tab has been changed, then send info to a Stringbuilder etc. But I can never seem to get it to call properly, I have tried delegating the genreViewController and tried to use the protocols but it never calls...

Please tell me what I need to put inside of the tab bar function since I do not know what should be there. I have tried putting the tabbarcontroller there and select viewcontroller. I am very new to all this so please dont be harsh :(

TabBarController:

import UIKit
class TabBarViewController: UITabBarController{

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func tabBar(_ tabBar: TabBarViewController, didSelect item: UITabBarItem) {
        print("he")
        //ask where it is first tab bar item
        if self.tabBarController?.selectedIndex == 1 {
            print("genres")

        }

    }

}

GenreViewController:

import UIKit

class GenreViewController: UIViewController,     UITableViewDelegate,UITableViewDataSource, UITabBarControllerDelegate {

    @IBOutlet weak var genreSwitch: UISwitch!

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.tabBarController?.delegate = self
        tableView.isScrollEnabled = false
        let tabBarViewController = TabBarViewController()



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    let genreArray = ["Action","Adventure","Comedy","Fantasy","Drama","Horror","Romance","Thriller","Family"]
    let genreIds = [28,12,35,14,18,27,10749,53,10751]
    //LATER!!: Save this Array until next time they log into the app
    var activeGenreArray = [Int!](repeating: 0, count: 9)





    //Number of rows
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return genreArray.count
    }


    //What to do with tableview
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! GenreCustomcell


        cell.genreSwitch.isEnabled = true
        cell.genreName.text = self.genreArray[indexPath.row]
        cell.genreSwitch.setOn(false, animated: true)

        cell.genreSwitch.addTarget(self, action: #selector(GenreViewController.switchChange(sender:)), for: UIControlEvents.valueChanged)
        cell.genreSwitch.restorationIdentifier = "\(genreIds[indexPath.row])"
        cell.genreSwitch.tag = indexPath.row


        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func switchChange(sender: UISwitch){

        let id = Int(sender.restorationIdentifier!)
        let row = Int(sender.tag)
        print(row)

        if sender.isOn == true{
            activeGenreArray[row] = id!
            print(activeGenreArray)
        }
        else{

            activeGenreArray[row] = 0

            print(activeGenreArray)
        }

    }




}


class GenreCustomcell: UITableViewCell{

    @IBOutlet weak var genreName: UILabel!

    @IBOutlet weak var genreSwitch: UISwitch!

}

Upvotes: 0

Views: 675

Answers (1)

Jonah
Jonah

Reputation: 17958

It's not clear to me what you are trying to do here. You've declared your GenreViewController to be a UITabBarControllerDelegate but then implemented a UITabBarControllerDelegate method (tabBar:didSelect:) on your TabBarViewController subclass of UITabBarController.

I would expect to see you have:

  1. a reference to some UITabBarController (this could be more specifically a TabBarViewController)
  2. a reference to some UITabBarControllerDelegate (this could be a GenreViewController, a TabBarViewController if it should be its own delegate, or some other object entirely)
  3. to set the UITabBarController instance's delegate property to be the UITabBarControllerDelegate instance.

Things that may have confused you:

  • A view controller can exist (and be associated with a tab) but does not load it's view until it is needed (i.e. you are switching to its tab). If you added your GenreViewController to a tab bar controller it's viewDidLoad method will not be called until the tab bar controller switches to that tab the first time. If your GenreViewController was setting its own tab bar controller's delegate in viewDidLoad (e.g. self.tabbarController?.delegate = self) it may not become the tab bar controller's delegate when you expected and so would not be notified of changes in the selected tab until after it had been selected once.
  • It's not clear what the let tabBarViewController = TabBarViewController() line is meant to do but that is creating a new TabBarViewController which is not the one you might already be seeing in your window. Creating a new instance of a class is not equivalent to referencing some existing instance of that same class.

Upvotes: 1

Related Questions