AmazingKoala
AmazingKoala

Reputation: 1

Dark and light not switching with segmented control (Swift)

Well I'm trying to make a light and dark theme for a cookie clicker with segmented index but when i switch nothing happens and im really confused if anyone could help me i would really appreciate it.

Ive tried changing else if. Removing code just trying print statements in it its ad if the function

//
//  ViewController.swift
//  clicker
///Users/ishaanrao/Desktop/Swift/clicker/clicker/Base.lproj/Main.storyboard
//  Created by Ishaan Rao on 8/1/19.
//  Copyright © 2019 Ishaan Rao. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    //Properties

    var x: Int = 0
    @IBOutlet weak var selector: UISegmentedControl!
    @IBOutlet weak var resetbtn: UIButton!
    @IBOutlet weak var score: UILabel!
    @IBOutlet var back: UIView!

    //viewDidLoad

    override func viewDidLoad() {
        super.viewDidLoad()
        resetbtn.backgroundColor = UIColor.black
        resetbtn.setTitle("Reset", for: .normal)
        resetbtn.setTitleColor(.white, for: .normal)
        resetbtn.layer.masksToBounds = true
        resetbtn.layer.cornerRadius = 5
        score.backgroundColor = UIColor.blue
        score.layer.masksToBounds = true
        score.layer.cornerRadius = 5
        score.textColor = UIColor.white
        score.textAlignment = .center

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

    //methods

    @IBAction func cookie(_ sender: Any) {
        x += 1
        score.text = "Score: \(x)"
    }

    @IBAction func reset(_ sender: Any) {
        x = 0
        score.text = "Score: \(x)"
    }

    @IBAction func darl(_ sender: Any, forEvent event: UIEvent) {
    }

    @IBAction func light(_ sender: Any)  {
        if selector.selectedSegmentIndex==0 {
            resetbtn.backgroundColor = UIColor.black
            resetbtn.setTitle("Reset", for: .normal)
            resetbtn.setTitleColor(.white, for: .normal)
            resetbtn.layer.masksToBounds = true
            resetbtn.layer.cornerRadius = 5
            score.backgroundColor = UIColor.blue
            score.layer.masksToBounds = true
            score.layer.cornerRadius = 5
            score.textColor = UIColor.white
            score.textAlignment = .center
        } else if selector.selectedSegmentIndex==1 {
            back.backgroundColor = .black
            resetbtn.backgroundColor = UIColor.white
            resetbtn.setTitle("Reset", for: .normal)
            resetbtn.setTitleColor(.black, for: .normal)
            resetbtn.layer.masksToBounds = true
            resetbtn.layer.cornerRadius = 5
            score.backgroundColor = UIColor.orange
            score.layer.masksToBounds = true
            score.layer.cornerRadius = 5
            score.textColor = UIColor.black
            score.textAlignment = .center
        }
    }



}

I want it to change colors but its not there are no errors

Upvotes: 0

Views: 272

Answers (1)

alxlives
alxlives

Reputation: 5212

It appears that the IBAction just isn't linked to the button event. Try to link the Storyboard button TouchUpInside event to the action like this:

enter image description here

For more info, you can follow the Oficial Apple documentation

Upvotes: 1

Related Questions