I have a text field and a button. After the text is inside the text field rather then hitting return, can i hit the button that is near my text field and it will act like the 'return' or 'enter' key.
I have found a stackoverflow anwser, kinda like my question: But still doesn't work "How to click UIButton when user hits return on keyboard"
I just need the button to act like a 'Enter' or 'Return' button.
#IBAction returnButton(sender: UiButton) {
textFieldNextToButton.resignFirstResponder()
}
UPDATE: IN OBJ-C
(IBAction)Button1:(id) sender {
[currentTextField resignFirstResponder];
}
Related
I am making a custom keyboard that has a search bar. In order for a user to be able to interact with it, they must be able to:
1- Input text with individual letter buttons.
2- Tap the search button.
As for number 1, I have working IBActions that are called at every button press. For example, here's the action for the letter "m":
#IBAction func mPressed(button: UIButton) {
searchBar.text! += "m"
}
However, the search button IBAction will not call:
#IBAction func searchPressed(button: UIButton) {
print("searchPressed")
}
When I connect the search button in storyboard to the "mPressed" action, that action is called. But when I reconnect it back to the "searchPressed" function above, it doesn't work again.
I have also made sure that I correctly connected the button to the action.
The search action is connected to the search button
The 'm' action is connected to the 'm' button
Thanks!
In your screenshot search button connected to method searchButtonPressed, but in code you have method searchPressed.
Delete connection to searchButtonPressed and reconnect to searchPressed and it will work.
I want to know if there is a way to know the value of textfield before the value is changed to the new value.
For example:
If the text field had a value "a" and the user entered "b", so the editing changed event is fired. When I try to fetch the value of the text field in the debugger it will display "ab". Is there a way that I can get "a" before "b" was written?
Here is a piece of code where I am trying to fetch the value:
#IBAction func commentEditingChanged(sender: AnyObject) {
if(!((sender as? UITextField)!.text!.isEmpty)){
counter++
}
else
{
counter--
}
}
In this case I want to know if the text was empty before the user entered text because I don't want the counter increments every time the user enters a character. I want to increment once the user entered any text.
And please don't propose to use delegate function textField:shouldChangeCharactersInRange:replacementString because my question is about this IBAction method.
If there is no way to do that using this IBAction Method, is there is any other IBAction method that can achieve what i want?
How to disable a save button until all the fields are filled in so it prevents the user from continuing until they enter something.
How can I do that in Swift?
First write a method to check for your textfield's text and only enable the button if the text fields contain valid text:
func checkField(sender: AnyObject) {
if textField1.text.isEmpty || textField2.text.isEmpty
{
yourbutton.enabled = false
}
else
{
yourbutton.enabled = true
}
}
Then call your function from the text field's delegate method textFieldDidEndEditingso that when the user finishes editing a field the button is enabled/disabled correctly.
I'm learning OBJ.C/Xcode/Cocoa here and have run into a question I can't seem to find an answer for.
I want the text in a UILabel to switch between two states (ON/OFF for example) each time the user clicks the same button.
I can get the label to switch to one state, but can't get it to switch "back" when the user clicks the button again. I am assuming there is some logic required, or checking the status of the object once the button is clicked...
Does this require me to keep track of a bool or the "state" of the Label (or button)?
Would this require two methods to be associated to the button, or can it happen with just one?
Thanks for any guidance/pointing in the right direction/Code snips!!!!
~Steve
I got an answer, and it works:
- (IBAction)flip:(id)sender
{
_flipButton.selected = !_flipButton.selected;
self.flipLabel.text = (_flipButton.selected) ? #"ON" : #"OFF";
}
- (IBAction)flip:(id)sender {
[yourButton setSelected:!yourButton.selected];
self.flipLabel.text = (yourButton.selected) ? #"ON" : #"OFF";
}
This toggles the selected state of the button then checks its state to determine which string to pass to the text property of your label.
the easiest way (I think) is to store a BOOL with the state of the UILabel and (on each click of the button) negate the BOOL and set the appropriate text of the Label
You could call this: yourLabel.enabled = !yourLabel.enabled on button click to change the enabled-state of your UILabel. Or what kind of state do you mean ?
In jQuery Mobile there are filtered listviews. After entering text into filter, list gets filtered but pressing GO button doesn't hide the keyboard.
User expects that keyboard will hide after pressing GO.
Is there any way to accomplish that?
You have to remove the focus manually from the input field with Javascript like this:
$('.ui-listview-filter .ui-input-text').live('keyup', function(event) {
if (event.keyCode == 13) {
// 'Go' pressed
$('.ui-listview-filter .ui-input-text').trigger('blur');
}
});
You may want to try changing the focus after the key is pressed. For example:
//this will un-select the text box, hiding keyboard
$('#searchInputId').blur();
//this will focus on something else, hiding keyboard
$('#somethingOtherThanTheSearch').focus();
Variant of Pablo's answer which works on my outdated version of jQuery (mobile):
// this is here to close the on-screen keyboards of mobile devices when the "go" button is used
$("input[data-type='search']").on('keyup', function(event) {
if (event.keyCode == 13) { // Enter or 'Go' pressed
event.target.blur(); // remove focus on the search field so keyboard goes away
}
});