How to Hide the Keyboard When I Press Return Key in a Uitextfield

How to hide the keyboard when I press return key in a UITextField?

First make your file delegate for UITextField

@interface MYLoginViewController () <UITextFieldDelegate>

@end

Then add this method to your code.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {        
[textField resignFirstResponder];
return YES;
}

Also add self.textField.delegate = self;

How to hide keyboard when return key is hit - Swift

Your problem is that you didn't delegate a textField in order to use that method. First, your class must include the UITextFieldDelegate protocol:

class yourClass: UIViewController, UITextFieldDelegate { ... }

And in the viewDidLoad() method, add this as well:

scoreText.delegate = self

And then you have to change this:

func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing(true)
return false
}

to this:

func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing(true)
return true
}

Final code:

class yourClass: UIViewController, UITextFieldDelegate {

@IBOutlet weak var scoreText: UITextField!

override func viewDidLoad(){
super.viewDidLoad()
scoreText.delegate = self
}

func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing()
return true
}
}

If this is not working, the problem is not the textFieldShouldReturn() function. Please check your outlet connections.

Hide the keyboard with return key for an UITextField in a tableviewcell

Try adding the following function to the class that implements the UITextFieldDelegate:

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
textField.resignFirstResponder()

return true
}

Hide Keyboard on return key [UITextField] and call function after

The problem is that your changePassword method takes a long time to run, because it's talking to a server over the network.

All user interface updates, such as the keyboard hiding animation, are triggered from the main thread. When you call a slow method on the main thread, you prevent those user interface updates from happening.

You need to move the call to changePassword off the main thread. There are many ways to do this. One of the simplest ways is to use Grand Central Dispatch (GCD), like this:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self changePassword];
});

However, this is not safe unless your changePassword method is thread-safe, so you need to think about what you're doing in changePassword and what will happen it it's running on a background thread while other methods are running on the main thread.

You need to read the Concurrency Programming Guide, or watch some of the WWDC videos, like Session 211 - Simplifying iPhone App Development with Grand Central Dispatch from WWDC 2010.

How do i dismiss the keyboard on return key from a UITextView

Remember to add the UITextDelegate into ViewDidLoad()

 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n"
{
textView.resignFirstResponder()
return false
}
return true
}

Hide keyboard when hitting return key in IOS5

assuming you are using UITextField, how do you create the textfield? is it by xib? or by code? make sure you implement UITextFieldDelegate in your class

@interface YouClass : UIViewController <UITextFieldDelegate>

if its xib, connect your textField to the file owner's delegate. and also connect your File owner to your IBOutlet UITextField

enter image description here

if its by code. just do

yourTextField.delegate = self;

now implement

-(BOOL)textFieldShouldReturn:(UITextField *)textField

Hide the keyboard when Enter is pressed in a UITextField

Implement the UITextFieldDelegate protocol's textFieldShouldReturn: method, and return YES based on the object that is passed in.

- (BOOL) textFieldShouldReturn:(UITextField *) textField {
[textField resignFirstResponder];
return (textField == someTextField);
}

How to hide keyboard on return key in Sprite kit?

The line:

firstPlayer.delegate = self as? UITextFieldDelegate

should just be:

firstPlayer.delegate = self

The fact that you added the as? UITextFieldDelegate probably means that you forgot to indicate that your class conforms to UITextFieldDelegate.

Update your class declaration from something like:

class MyViewController: UIViewController {

to something like:

class MyViewController: UIViewController, UITextFieldDelegate {

Just add the , UITextFieldDelegate to whatever your current declaration is.

You also have a typo on:

func textFieldShouldReturn(_textField: UITextField) -> Bool {

That needs to be:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

Note the space after the _.



Related Topics



Leave a reply



Submit