Search code examples
iosswiftuikitviewwithtag

Why viewWithTag is returning nil


activite1Label as the tag 1

class StatsViewController: UIViewController {

    @IBOutlet weak var activite1Label: UILabel!
    @IBOutlet weak var activite2Label: UILabel!
    @IBOutlet weak var activite3Label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        activite1Label.text = activite[0]
        activite2Label.text = activite[1]
        activite3Label.text = activite[2]

        miseAjourTotal()

    }

    func miseAjourTotal() {        
        let leLabel = view.viewWithTag(1) as! UILabel
        print("leLabel: \(leLabel.text)")
    }
}

Solution

  • Nothing in your code tells that the label has the tag 1. You should go to your storyboard and check if the label does have tag 1 or set the tag programmatically