Reputation: 207
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
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:
UITabBarController
(this could be more specifically a TabBarViewController
)UITabBarControllerDelegate
(this could be a GenreViewController
, a TabBarViewController
if it should be its own delegate, or some other object entirely)UITabBarController
instance's delegate
property to be the UITabBarControllerDelegate
instance.Things that may have confused you:
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.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