Stop all UIButtons from working when I tap 1 UIButton swift - ios

I want to make it so that when I tap a UIButton, the rest of buttons are disabled until I click a refresh button.
I have 5 IBOutlets that I want to disable not including the refresh Button. Is there a function that stops the events that I can tie to the rest of the buttons? I don't even know where to start.
Thanks a lot in advance!

Connects a method (IBAction) to the button that must disable other.
Within the method, for each button (connected in the GUI) type the follow line:
button.enabled = false

Hey so I got it to work! I did some more research and there's a duplicate of this post. Everything is explained here:
Disable and Enable UIButton - XCode Swift
Thanks a lot anyways.

Related

How to make a UIButton upresponsive

I have a case where I would like to make an already existing UIButton unresponsive and basically act as a UILabel. Doesn't anyone know a way of doing this programmatically in c preferably?
Set it's .userInteractionEnabled property to false
If you disable Interaction still you can see the UIButton blinking for click. Better you can uncheck Enable button property from Accessibility.
:
This will help to achieve your requirement.
You can disable userinteraction on your button something like,
yourButton.userInteractionEnabled = NO;
And your button will not respond more!
Disbale User Intercation by writing this code:
yourButton.userinteractionenabled=NO;

How can I properly handle touch events of a UIButton on top of another UIButton?

I'm trying to recreate the function of the clear button in UITextFields but with a UIButton and as such have a UIButton as a subview of another UIButton. I read somewhere that I need to have the superview handle the touch events of the subview.
The post that hinted at that was outdated and in obj-C so I'm looking for modern and swift version.
Placing a button on another button is a kind of bad implementation in spite of any requirements.
Hope you find out a right way to solve this problem.
Or you can just use a combination of enabling or disabling buttons according to the situation.

Highlight UIButton when not selecting the button itself

I have a custom class here that consists of a UIButton inside of a UIView.
The goal of this class is to allow users to have a greater area (being the UIView) to select the button than just the buttons frame itself.
Now when a user taps on the view I want the buttons highlighted image to show... But the problem is, it does not.
I've read many possible solutions to this issue For Example:
Calling: [btnObject sendActionsForControlEvents:UIControlEventTouchUpInside]
This however did not change the buttons highlight.
I also tried just settings the button.highlighted = YES;
But that did not work either.
I have the images for different states properly setup (Normal and Highlighted), I'm sure of that.
I also have the gestureRecognizer working properly as the functionality is great except for the lack of highlight.
Does anybody know if I'm missing any thing special that needs to be done in order to pull off this seemingly very simple task? Surely it's been done many times.
Thank you
You were on the right track. -[UIButton setHighlighted:] is just a flag. What you need to do is call setNeedsDisplay on that button right after you change the highlighted property.
I solved my problem a little while ago and I'm not sure if Kevin Low's answer would've worked too, it very well might have.
But for some reason, a UITapGesture doesn't work well with highlighting buttons as a view transitions (That might be because I didn't call setNeedsDisplay). The gesture that ended up working was the UILongPressGesture with a 0.0 sec for minimum duration.

Swift - how to hide unknown button?

I have 4 buttons. Each of them represent 1 answer for question in trivia game. In different screens i have different questions and buttons with different answers. I have a mode, when when player can make a mistake, this is enabled by pressing the fifth button. So, when this mode is enabled i show alert and i need to hide button with wrong question which was pressed. Everything that i know about - that it is member of array with buttons with wrong questions. How can I tell the alert which button to hide on completion ?
Attach them all to an IBAction that takes a UIButton and then hide the one that was pushed by adding the line
sender.hidden = true
or disable it by adding
sender.enabled = false
Do you mean you wish to know if the button has been pressed? Well if you connect that UIButton to that IBAction, that IBAction will be fired each time the button is pressed. You could insert and NSLog("button press") line inside the IBAction to see that it is pressed.

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Resources