The textview keyboard does not resign when I press iOS

I have a text view, and when I click, textview starts a new line.

I implemented these methods and added all the delegates

@property (nonatomic, strong) IBOutlet UITextView *textField; -(void)textViewDidEndEditing:(UITextView *)textView //was UITextField { [self.textField resignFirstResponder]; } - (BOOL)textFieldShouldReturn:(UITextView *)textField{ [textField resignFirstResponder]; return YES; } 

I set the keyboard return key to the attribute handler. I want the key to work on the keyboard to cancel the first responder, it doesn't seem to work.

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; // error thrown here saying code will never execute if(range.length + range.location > textField.text.length) { return NO; } NSUInteger newLength = [textField.text length] + [string length] - range.length; return newLength <= 300; } 

How can I add both operators so that they work without errors?

+4
source share
2 answers

add delegate UITextViewDelegate

 @property (nonatomic, strong) IBOutlet UITextView *textField; 

Write this line to viewDidLoad

 textField.delegte = self; 

// Add the delegate method below [the code is in Swift. find the obj-c method and write the logic like me]

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } if (textField.text.length >= 300) { return NO; } return YES; } 
+2
source

Go to your story panel, then select your text box and set the delegate, and also confirm the UITextFieldDelegate protocol in the .h file

Hope this helps.

+2
source

All Articles