You have a few problems here:
What is causing the crash is that MarketButton
requires you to pass in a UIButton as a parameters which the timer is not doing when triggered. You can do this by passing in the button through the userInfo parameter of scheduledTimer
. Here is a stack overflow post on how to do that.
However, even after you do this things will still not work.
You need to make a method to handle when the timer is done.
Something like:
func enableButton() {
yourButton.isEnabled = true
}
And then put that as the selector instead of the MarketButton
method.
like so:
let timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(enableButton), userInfo: nil, repeats: false)
By putting "MarketButton" as the selector of the timer you are causing an infinite loop. When the timer is done it calls the method which then triggers another timer and so on.
Another issue is with these two lines of code:
let timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(MarketViewController.MarketButton), userInfo: nil, repeats: false)
sender.isEnabled = !(timer.isValid )
In this case timer will almost always be valid because you just set it. So !(timer.isValid)
will basically always return false. However, if you follow my advice and trigger a different method instead of MarketButton
then this will not be a problem.
Also a side note, when naming functions you should not capitalize them so MarketButton
should be marketButton
.
I suggest trying to find the solution with the information I gave you. If you have any questions let me know and welcome to stack overflow!