I want to disable other buttons when I press the button.

Asked 2 years ago, Updated 2 years ago, 71 views

When I press stopButton, I want to disable nextButton and backButtom. What should I do?

IBAction func nextButton(_sender:Any){
    if imageIndex==2 {
        imageIndex=0
    } else {
        imageIndex= imageIndex+1
    }
    imageView.image=images [imageIndex]
}

@ IBAction funcbackButtom(_sender:Any){
    ifself.imageIndex==0{
        imageIndex=2
    } else {
        imageIndex= imageIndex-1
    }
    imageView.image=images [imageIndex]
}

@ IBAction func stopButton(_sender:Any){
    timer=Timer.scheduledTimer(timeInterval:2, target:self,
                                 selector:# selector(self.movePhoto(_:), userInfo:nil, repeat:true)
}

swift4

2022-09-29 20:28

1 Answers

I'd like to disable nextButton and backButtom, what should I do?

Only a part of the code appears in your question, but have you declared and connected the outlet variable for the two buttons? If not, add the following declaration to connect as outlet in the storyboard editor:

class ViewController:UIViewController {

    @IBOutlet weak var theNextButton: UIButton!
    @IBOutlet weak var theBackButton: UIButton!

    //...
}

< If the ViewController is different from your actual class name, read it properly.

< The identifiers nextButton and backButton are used in the action method, so I did theNextButton, theBackButton to avoid wearing it.Actually, I think it would be better to change the method name to "action method-like name", but once I change the method name of the connected action method, I might step on an Xcode bug, so I fiddled with the outlet name.

With this in mind, disabling the normal UIControl simply requires the value of the isEnabled property to be false.

@IBAction func stopButton(_sender:Any){
    timer=Timer.scheduledTimer(timeInterval:2, target:self,
                                 selector:# selector(self.movePhoto(_:), userInfo:nil, repeat:true)
    theNextButton.isEnabled=false
    theBackButton.isEnabled=false
}

Try it.


2022-09-29 20:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.