Xamarin Begineer
Xamarin Begineer

Reputation: 35

How to Disable UIButton with a Timer

I would like to make the button disable for 10 second before letting user to click the button. Any idea how to do that?

Below attached the method of the button:

partial void CheckOutBtn_TouchUpInside(UIButton sender)
    {
        if(condition)
        CheckOutBtn.Hidden = true;
        else
            new UIAlertView("Great Job!", "You had checked out.", null, "OK", null).Show();
    }

Upvotes: 1

Views: 105

Answers (1)

SushiHangover
SushiHangover

Reputation: 74094

when user enter the page, the check out button will be disable for 10 second, after that only appear and let user to click it.

You are saying "disable" and "appear", two different things, but you can use the GCD to execute a block of code after a delay.

Something like this in your ViewController's ViewDidLoad() override will get you started:

~~~
//? button.Hidden = true;
button.Enabled =  false;

var delay = new DispatchTime(DispatchTime.Now, TimeSpan.FromSeconds(10));
DispatchQueue.MainQueue.DispatchAfter(delay, () =>
{
    button.Enabled = true;
    //? button.Hidden = false;
});

button.TouchUpInside += (object sender, EventArgs e) =>
{
    // perform some action... 
};
~~~

Upvotes: 1

Related Questions