0

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.

1 Answers1

3
@IBAction returnButton(sender: UiButton) {
    textFieldNextToButton.resignFirstResponder()
}

UPDATE: IN OBJ-C

(IBAction)Button1:(id) sender {
    [currentTextField resignFirstResponder];
}
Tim
  • 2,089
  • 1
  • 12
  • 21
  • Where should i put this code? Every where i try to put it now, says "Unexpected '@" in program". – iIveTh3PaST Nov 11 '15 at 01:56
  • You ctrl-drag from the button in Storyboard (assuming you're using storyboard), to anywhere in the ViewController (but not inside a function). It creates the @IBAction for you. Alternatively, if you already had code running from the button, put the `textField.resignFirstResponder()` in that function. – Tim Nov 11 '15 at 02:05
  • See this for more on IBAction http://stackoverflow.com/questions/32572305/iboutlet-and-ibaction-in-swift – Tim Nov 11 '15 at 02:06
  • i created a 'IBAction' for the button. This piece of code was created too " -(IBAction)Button1:(id) sender {} – iIveTh3PaST Nov 11 '15 at 02:08
  • So i put the code "textFieldNextToButton.resignFirstResponder()" into the IBAction code. But i get error: "Property access result unused - getters should not be used for side effects" – iIveTh3PaST Nov 11 '15 at 02:09
  • You're writing in Obj-C, my answer was in swift (sorry you didn't tag the language). You need to figure out how to do `resignFirstResponder` inside that block that it gave you. It won't be the same syntax. – Tim Nov 11 '15 at 02:10
  • Added the inside line in Obj-C – Tim Nov 11 '15 at 02:11
  • That works for dismissing the keyboard. But i'm trying to use the button to "Enter". So when the text is in the text field, instead of hitting enter on my keyboard, i can press that button. How should i do this? – iIveTh3PaST Nov 11 '15 at 02:18
  • What does hitting the enter button do? – Tim Nov 11 '15 at 02:19
  • You can add other code in that same block, such as saving the currentTextField text to a variable. – Tim Nov 11 '15 at 02:20
  • Well... I have a else if statement... When you type the different words in the text field it will run different 'else if' statements. But they won't run unless i hit 'Enter' on my keyboard or 'Return' on the iOS keyboard while still on the textfield. Even tho the code you provided me, dismisses the keyboard. I need it to be able to enter, so the text will be pushed to the action. – iIveTh3PaST Nov 11 '15 at 02:24
  • What method is calling that else-if statement? Is it in textFieldDidChange or textFieldDidEndEditing? The last should be called when resigning first responder. Otherwise, you could also put your code inside this new block, or call the function from there. – Tim Nov 11 '15 at 02:29
  • here is some example code of my 'else if' -(void) perform: (NSString *) command { // WithOut WhiteSpace (Text Control) if([command caseInsensitiveCompare:@"ROTATE"] == NSOrderedSame) // Command Rotate { [pattern decreaseAngle]; int f = [pattern getAngle]; [self changeAngle:(f)]; } – iIveTh3PaST Nov 11 '15 at 02:33
  • But how does that get triggered by `return`? – Tim Nov 11 '15 at 02:38
  • I'm not for sure. But it does... Thats why i got to get the button to act like a 'return' – iIveTh3PaST Nov 11 '15 at 02:40
  • is their any other methods for making a button act like a 'return' key? – iIveTh3PaST Nov 11 '15 at 02:41
  • I can't give you any more help without seeing the whole code. There are many ways to pick up that signal, and you could do it in the block I provided. You just have to call your needed code from there. – Tim Nov 11 '15 at 02:45