Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In the past, to dismiss the keyboard after a user hits return I would simply override the TextViewShouldReturn function. However, upon re-downloading Xcode (version 9.3.1) it seems it is no longer a function within UITextViewDelegate and most all previous questions I've found on the subject advise overriding TextViewShouldReturn in some way as well. Is there something I've forgotten or a more efficient way possibly?
This is what I'm currently looking at in my application:
There is no method like textViewShouldReturn() for textView like textField, rather you can use the following code to return your keyboard-
extension ViewController: UITextViewDelegate {
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return true
}
return true
}
// Make sure you have self the textView delegate in viewDidLoad method
Maybe you're confusing UITextViewDelegate with UITextFieldDelegate?
There is no function textViewShouldReturn in UITextViewDelegates this function is of UITextField delegate so you have to create your own code for hide keyboard when click on return. By default in UITextView return button is used for next line.
if you want to hide keyboard .try this
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
if ( [ text isEqualToString: #"\n" ] ) {
[ textView resignFirstResponder ];
return NO;
}
return YES;
}
Related
This question already has answers here:
How to make return key on iPhone make keyboard disappear?
(14 answers)
Closed 6 years ago.
I have a button that when pressed brings up a keyboard for a textfield:
- (IBAction)textButtonPress:(id)sender {
[self.textField becomeFirstResponder];
}
The problem I have is that when I press the return button on the keyboard nothing happens. How can I make the keyboard automatically close when the return key is pressed?
Add this in your ViewController class
yourTextField.delegate = self
and this UITextFieldDelegate method
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
I'm creating an iOS 8 app with Xcode 6.0.1 for my iPhone 5 (which has iOS 8.0.2 on it). I want to make it so that when a user clicks on my UITextView, all the text gets selected so he can easily start typing and erase what was there (but I don't want the text to be automatically erased because the user may want to keep it or append to it). To do this, I have the following code:
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView hasText]) {
NSLog(#"selectedRange before: %d", textView.selectedRange.length);
[textView selectAll:self];
NSLog(#"selectedRange after: %d", textView.selectedRange.length);
}
}
When this method gets called, the console output is what I expect (i.e. the selectedRange length is the same as the number of characters in the textView's text). However, nothing shows up as selected in the UITextView and it doesn't act selected (i.e. no selection menu pops up).
I have seen multiple questions like this on the internet, but none of the provided solutions worked for me (and some of them wrote it off as a bug without providing any solution). Changing the sender id to something other than self (such as nil) did not help, and neither did it help to call [textView select:self] as one person suggested. I have also tried this code:
- (void)textViewDidBeginEditing:(UITextView *)textView {
if ([textView hasText]) {
UITextRange *range = [textView textRangeFromPosition:textView.beginningOfDocument toPosition:textView.endOfDocument];
[textView setSelectedTextRange:range];
}
}
But, it has the same problem.
Any suggestions?
This solution works too and does not require subclassing UITextView, just put this function on your delegate:
OBJECTIVE C -
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
[textView selectAll:nil];
});
return YES;
}
SWIFT 3 -
func textViewDidBeginEditing(_ textView: UITextView) {
DispatchQueue.main.async {
textView.selectAll(nil)
}
}
#brentvatne 's solution worked for me. Posting the Swift syntax so people can copy and paste in the future.
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
dispatch_async(dispatch_get_main_queue()) {
textView.selectAll(nil)
}
return true
}
The best solution I've found for this issue so far is to create a custom UITextView (i.e. create a new class that extends UITextView) and then implement the selectAll method like this:
- (void)selectAll:(id)sender {
[super selectAll:sender];
UITextRange *selectionRange = [self textRangeFromPosition:self.beginningOfDocument toPosition:self.endOfDocument];
[self performSelector:#selector(setSelectedTextRange:) withObject:selectionRange afterDelay:0.0];
}
Then when you use a text view, set its type to your custom text view type (in your code and in the storyboard). Now you can successfully call the selectAll method whenever you need to. I suppose this should work with UITextField too, but I haven't tried it yet.
This question already has an answer here:
resignFirstResponder for all textfields [duplicate]
(1 answer)
Closed 8 years ago.
I know that I can resign a single text field to first responder when touching a button by implementing the following:
- (IBAction)signMeUpButton:(id)sender {
[self.textfield resignFirstResponder];
}
However, In my case I have multiple text fields, and I feel like typing them in one at a time, cant possibly be the best way / most modern way of doing so.
I reviewed multiple questions on the site, such as this one:
iOS SDK: Dismiss keyboard when a button gets clicked?
But none of them mention, having more than one text field and dismissing them with an IBAction ... What can I do in this situation?
You may try this:
- (IBAction)signMeUpButton:(id)sender
{
[self.view endEditing:YES];
}
If you have more than one text field, it's best to implement the following
[self.view endEditing:YES];
So, in your case it would be :
- (IBAction) signMeUpButton:(id)sender {
[self.view endEditing:YES];
}
That will do exactly what you are looking for!
try this
UITextField *currentTextField;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
-(IBAction)click:(id)sender
{
[currentTextField resignFirstResponder];
}
I have implemented UITextViewDelegate in my ViewController,
After setting delegate to my TextView as
self.addressTextView.delegate=self;
Now i can only set the text as,
[self.addressTextView setText:#"Tamil Nadu, India"];
I am unable to edit the text using keybord. After Implementing shouldChangeTextInRange method only i am able to edit the content in UITextView.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
I dont know why?? is shouldChangeTextInRange compulsory if we implementing UITextViewDelegate
NO. Its not compulsary. You can set text like that
textView.text = #"Hello";
This method will be call when you are try to write somthing in your text view.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Its not compulsory in the protocol definition, so more than probably something else is going on. From reading that implementing the delegate method mentioned fixes this, its quite possible that your controller's superclass implements the method and only allows the text you've got above. If that's the case, the workaround you've implemented is in fact the easiest way to fix this.
For your information, everything that must be implemented when you declare that a class follows a protocol will be indicated by the compiler : you will get an error when you build if a method or a property is missing.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have three text fields and I want to be able to move the cursor to the next one. When the last one is reached, the keyboard dismisses. But nothing works for me... Here's an example of what I have. However, nothing moves, and nothing happens when I select the next button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == field1TextField) {
[textField resignFirstResponder];
[field2TextField becomeFirstResponder];
}
else if (textField == field2TextField) {
[textField resignFirstResponder];
[field3TextField becomeFirstResponder];
}
else if (textField == field3TextField) {
[textField resignFirstResponder];
}
return YES;
}
The object that this method is bound to should be set as the text field's delegate. Check this by setting a breakpoint in the method to verify that it is called when you think it is.
If you are using nibs or storyboards, the field... instance variables should be outlets that are correctly hooked up. If they are created programmatically, you should ensure that they have objects assigned to them. Verify this by inspecting the values of these variables when you are inside the method.
You don't have to call resignFirstResponder on other controls before calling becomeFirstResponder on another.