Home Protocol and delegate not working
Reply: 1

Protocol and delegate not working

Matt Eng
1#
Matt Eng Published in 2018-01-13 05:30:37Z
import UIKit

class TabBarViewController: UITabBarController, selectedTopic {

    func selectedTopic(topicIndex: Int) {
        self.selectedIndex = topicIndex
        print(self.selectedIndex)
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        let menuViewController = MenuViewController()
        menuViewController.delegate = self

        tabBar.isHidden = true

    }



}

import UIKit

protocol selectedTopic {
    func selectedTopic(topicIndex : Int)
}

class MenuViewController: UIViewController {

    @IBOutlet weak var TapViewButton: UIView!
    @IBOutlet weak var UpgradeViewButton: UIView!
    @IBOutlet weak var TopicTitle: UILabel!
    @IBOutlet weak var LeaderboardViewButton: UIView!
    @IBOutlet weak var ShopViewButton: UIView!
    @IBOutlet weak var StatisticsViewButton: UIView!

    var delegate : selectedTopic?


    override func viewDidLoad() {
        super.viewDidLoad()


        delegate?.selectedTopic(topicIndex: 0)

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(changeToTapSegue))
        TapViewButton.addGestureRecognizer(tapGesture)

        let upgradeGesture = UITapGestureRecognizer(target: self, action: #selector(changeToUpgradeSegue))
        UpgradeViewButton.addGestureRecognizer(upgradeGesture)

        let leaderboardGesture = UITapGestureRecognizer(target: self, action: #selector(leaderboardViewTouched))
        LeaderboardViewButton.addGestureRecognizer(leaderboardGesture)
        let shopGesture = UITapGestureRecognizer(target: self, action: #selector(shopViewTouched))
        ShopViewButton.addGestureRecognizer(shopGesture)
        let statisticsGesture = UITapGestureRecognizer(target: self, action: #selector(statisticsViewTouched))
        StatisticsViewButton.addGestureRecognizer(statisticsGesture)

    }

    @objc func changeToTapSegue() {

        self.performSegue(withIdentifier: "TapSegue", sender: self)
    }

    @objc func changeToUpgradeSegue() {

        self.performSegue(withIdentifier: "UpgradeSegue", sender: self)
    }

    @objc func leaderboardViewTouched() {

        TopicTitle.text = "Leaderboard"


    }

    @objc func shopViewTouched() {

        TopicTitle.text = "Shop"
    }

    @objc func statisticsViewTouched() {
        TopicTitle.text = "Statistics"
    }

}

When the code is ran the view controller doesn't receive the data. It doesn't even run. I check this by placing print(self.selectedIndex) in the the function that receives the data. Nothing was printed in the console. Can you please help me solve this problem. I feel like it has something do to with setting the delegate. Can you please help me find the problem so I can fix it.

Chan Jing Hong
2#
Chan Jing Hong Reply to 2018-01-13 08:03:17Z

You created an object (MenuViewController) inside a function, and didn't keep a reference of it anywhere. This caused the object to be released at the end of the function, and it doesn't exist anymore.

Your problem is in the viewDidLoad() of TabBarViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    // Here you created a MenuViewController in viewDidLoad()
    let menuViewController = MenuViewController()
    menuViewController.delegate = self

    ...

    // By the end of viewDidLoad() your MenuViewController 
    // would already been released. And does not exist anymore.
}

What you should've done is to store your MenuViewController outside of your viewDidLoad():

var menuViewController: MenuViewController! 

override func viewDidLoad() {
    super.viewDidLoad()

    menuViewController = MenuViewController()
    menuViewController.delegate = self
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319806 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO