Swift - how to hide unknown button? - ios

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.

Related

How do I toggle hidden of a label while a button is pressed?

I am trying to figure out how to only display a label while a button is pressed in OS. I know how to operate the touch events but I am not sure how to incorporate the UILongPressGestureRecognizer into this.
The UIButton class, as well as lots of other UIControl subclasses can have numerous actions hooked up to them.
When we are hooking up an action from interface builder to our source code file, if we open the "Event" drop down, we're presented with a long list of options:
In almost every scenario, we're hooking our actions only up to "Touch Up Inside". This allows the user to consider whether or not they want to really press the button. If they drag their finger off the button before letting go, the action doesn't fire, because the "up touch" gesture happened outside the bounds of the object.
But here, we want to actually hook our button's "touch down" event up. This is when we'll display the label.
Let's go ahead and create a "touch down" event and a "touch up inside" event:
Swift
#IBAction func buttonTouchDown(sender: UIButton) {
self.myLabel.hidden = false
}
#IBAction func buttonTouchEnded(sender: UIButton) {
self.myLabel.hidden = true
}
Objective-C
- (IBAction)buttonTouchDown:(UIButton *)sender {
self.myLabel.hidden = NO;
}
- (IBAction)buttonTouchEnded:(UIButton *)sender {
self.myLabel.hidden = YES;
}
So far, buttonTouchEnded is set up completely normally, and buttonTouchDown was set up by selecting "touch down" from the "Event" list.
We can always verify what our control is hooked up to by right clicking it in the interface builder:
But this menu is useful for more than simply checking what we've already hooked up. From here, we can hook up any of the other actions to our existing #IBAction methods simply by clicking in the circle and dragging to the existing method.
So we obviously want the label to disappear if we stop pressing the button, a normal touch up like you'd hook up any other button. The only question remaining is, what exact behavior do you want?
If you want the label to disappear only when the finger is lifted, no matter where the finger goes, then we must also hook up "touch up outside".
If you want the label to disappear when the user drags their finger off the button, then we should hook up the "touch drag exit" action.
We also probably want to hook up the "touch cancel" action, which would occur if some sort of system event (perhaps an incoming phone call) cancels the touch.
This Stack Overflow answer elaborates on the differences between the action options we have, so you can craft the behavior exactly how you need it.
Anyway, once we decide which actions we want to hook up to which methods, bring up that right click menu and click-drag from the circles to the methods:
The easiest thing to do would be to add an action to the touchDown event and a separate action to touchUpInside and touchUpOutside.
Show the label on the touchDown action and hide it on the touchUpInside / touchUpOutside action. (and for completeness, on touchCancel, as suggested by nhgrif in his very thorough answer.)
A long press gesture recognizer won't work in this situation. You could create a custom gesture recognizer that triggered one event on touch and another event on release, and use that. It's actually not that hard to do.
EDIT
I just uploaded a demo project to GitHub called "MorphingButton" (link) that I created for another question here on Stack Overflow.
That project now shows a label on touching the app button and hides the label when you release the button.
The project is a hybrid Swift/Objective-C project that shows how to do the button morphing and label showing/hiding in both languages. It has a tab bar with a Swift tab and an Objective-C tab.

Stop all UIButtons from working when I tap 1 UIButton swift

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.

UIButton disabled in IBAction re-enables itself

I have a UIButton that is linked to an IBAction in a UIView. Within this IBAction, I both change the text of the UIButton's titleLabel and disable the UIButton. However, I've found that when the action fires, the button disables itself and changes its text for a split second before re-enabling itself. It's almost like it's happening just while the button is pressed, then it undoes itself. Could someone help with this? If it's useful, I'm on the latest Xcode and iOS 8 SDK.
//this is the IBAction...it happens, but the enabled property and text change quickly undoes itself.
#IBAction func solvePuzzle(sender: AnyObject) {
self.activity.startAnimating()
self.solveButton.enabled = false
self.solveButton.titleLabel?.text = "Solving..."
}
The text of the button should not be set like that. I am not very familiar with swift, but the right method should look something like:
self.solveButton.setTitle("Solving...", forState:UIControlState.Normal);
And also make sure that the method is called on the touchUpInside action.

iOS: UIButton touch up inside doesn't respond multiple press

I have a UIButton whose IBAction has been setup though XIB file. Following is the IBAction method. When I press button multiple times very quickly, this method gets called only once for the last press. I have to wait little more time between the presses. Is there anything like long press time for UIButton that I can reduce or any other settings to make it responding quicker. This button does the similar job of Keyboard's back button to delete last character. I want it to respond very quick like Keyboard's button does. Thanks.
- (IBAction)deleteButtonPress:(id)sender {
NSLog(#"Click");
if(self.numpadTextFiled.text.length > 0)
self.numpadTextFiled.text = [self.numpadTextFiled.text substringToIndex: [self.numpadTextFiled.text length]-1];
}
Is the button by any chance positioned within a UIScrollView? If so, then touches are delayed?

How do I make the keyboard go away when the user clicks somewhere else? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dismiss keyboard by touching background of UITableView
How do I make the keyboard go away when the user clicks somewhere else?
Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.
I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.
So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.
Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?
There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.
You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,
-(void) hideKeyboard {
[textFieldName resignFirstResponder];
[textFieldSurname resignFirstResponder];
for (UITextField * txtField in arrTextFields) {
[txtField resignFirstResponder];
}
}
Then, at various sections in your class, depending on the functionality required, call;
[self hideKeyBoard];
This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.
How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.
Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

Resources