-1

I got an issue. I want to set a timer that could disable the 6 large buttons of my ViewController2.

For example : Until the timer reach 100, it's not possible to click on the buttons Clue1Button,Clue2Button,Clue3Button,Clue4Button,Clue5Button,Clue6Button Until the timer reach 200, it's not possible to click on the buttons 2,3,4,5,6 ...

How should I do it ? I tried several times, but I failed each time. Thanks for your help

Code of my ViewController2 :

//  ViewController2.swift
//  PROJET X
//
//  Created by Alexis Decloedt on 22/12/2019.
//  Copyright © 2019 Alexis Decloedt. All rights reserved.
//

import UIKit




class ViewController2: UIViewController {
    @IBOutlet weak var Clue1Button: UIButton!
    @IBOutlet weak var Clue2Button: UIButton!
    @IBOutlet weak var Clue3Button: UIButton!
    @IBOutlet weak var Clue4Button: UIButton!
    @IBOutlet weak var Clue5Button: UIButton!
    @IBOutlet weak var Clue6Button: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()


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

  @IBAction func ChestButton(_ sender: Any) {
        dismiss(animated: false, completion: nil)
    }

    }
//
//  ViewController2.swift
//  PROJET X
//
//  Created by Alexis Decloedt on 22/12/2019.
//  Copyright © 2019 Alexis Decloedt. All rights reserved.
//

import UIKit

var currentCount : Int? 
var maxCount : Int?
var mytimer : Timer?
let ValeurStock = "ValeurStock"


class ViewController2: UIViewController {
    @IBOutlet weak var Clue1Button: UIButton!
    @IBOutlet weak var Clue2Button: UIButton!
    @IBOutlet weak var Clue3Button: UIButton!
    @IBOutlet weak var Clue4Button: UIButton!
    @IBOutlet weak var Clue5Button: UIButton!
    @IBOutlet weak var Clue6Button: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()

    currentCount = 0

        self.mytimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(increase), userInfo: nil, repeats: true)


    }

  @IBAction func ChestButton(_ sender: Any) {
        dismiss(animated: false, completion: nil)

    }

    func increase() {
        currentCount! += 1

    }

        func buttonStatus() {
            if currentCount ?? <#default value#> >= 100 {
            //I'm stuck ?? How to continue ?
            }
        }


    }


Try to make my button disable/enable
  • 2
    Your code doesn't have any timer. Can you show your attempt to use a timer and explain the specific problem you had? – Paulw11 Dec 23 '19 at 21:44
  • Whenever I tried to set a timer, my app would crash. I guess I did it wrong, so I pulled out the timer part to show something clean and start from scratch @Paulw11 I followed this video : https://www.youtube.com/watch?v=gPr4qQhz6SU – Alexis Decloedt Dec 23 '19 at 21:46
  • 1
    Please show the code you tried and the crash you got. – Paulw11 Dec 23 '19 at 22:19
  • I'm completely stuck @matt And your answer does not help me. I don't expect that from someone, i want to learn first. I wanted to know if someone have already written a similar kind of code, that could bring me some answers elements. – Alexis Decloedt Dec 24 '19 at 00:18
  • @matt I edited my post. thank you for your reply I am not sure of my edit code. My different problems are: - How can I correctly store a value from my timer? - How to start my timer correctly -> Some difficulties to understand what #selector is talking about? - And then how to activate / deactivate a button according to my storage value? I managed to enable / disable my buttons today, but I am blocked when I have to implement it in a function. It doesn't seem logical to me: / – Alexis Decloedt Dec 24 '19 at 01:14

1 Answers1

0

What you may want to do is:

//Add variable that will count how many times Timer was repeated.
var timerCount = 0

//Create timer
Timer.scheduledTimer(withTimeInterval: 100, repeats: true) { t in

    //After first 100 sec passed, timer will get triggered and enable the buttons you want.
    button1.isEnabled = true
    button2.isEnabled = true

    //After another 100 passed, timer will enable other buttons
    if timerCount == 1 {
        button3.isEnabled = true
        button4.isEnabled = true
        t.invalidate()
    }

    //Adds 1 to timer count
    timerCount += 1
}
Shalugin
  • 1,092
  • 2
  • 10
  • 15
  • Thank's for you reply @Shalugin . I have some questions about what you write. ``` //After first 100 sec passed, timer will get triggered and enable the buttons you want. button1.isEnabled = true button2.isEnabled = true``` How do you know that de button will be immediately enable after 100 sec passed. For me in this case, it will be automatically enable without waiting 100 sec. What goin to happen when the timer will be > 1 ? You don't have to target the counter when you launch the timer ? You don't need to make a function to increment the var timerCount ? – Alexis Decloedt Dec 24 '19 at 07:08
  • Not exactly. If you set Timer like this, it won't get triggered immediately. First time it gets triggered will be only after first 100 sec passed. When it happens it enables the first bunch of buttons. Then it waits another 100 sec and enables second bunch etc. To deactivate Timer you use t.invalidate(). So in my example when Timer finishes its second round after 200 sec in total, var timerCount will be equal to 1 and it will trigger if statement, which will enable last bunch of buttons and after that invalidate the timer. – Shalugin Dec 24 '19 at 08:00
  • Do you have an instant messaging to discuss easier. I've tried your solution but my button are enable immediately without waiting 100 seconds. Then after a few time the app crash – Alexis Decloedt Dec 24 '19 at 08:27
  • "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value" – Alexis Decloedt Dec 24 '19 at 08:31