Why does this only work sometimes? (UITextView resign first responder question)
When a user presses the "SEND"(return) button I want the keyboard to retract and do other stuff like send a message. But it only works SOMETIMES...
Why does this code only work SOMETIMES? I need it to work all the time obviously, but it doesn't.
- (void)textViewDidChange:(UITextView *)inTextView {
NSString *text = myTextView.text;
if ([text length] > 0 && [text characterAtIndex:[text length] -1] == '\n') {
myTextView.text = [text substringToIndex:[text length] -1];
开发者_Go百科 [myTextView resignFirstResponder];
[self sendMessage];
}
}
It only gets called if
([text length] > 0 && [text characterAtIndex:[text length] -1] == '\n')
is true. Is this always the case? Maybe add an NSLog statement outside and inside the state to see if it is indeed true all of the time.
Checking for \n looks bizarre. Why would you do that?
This method is getting passed in a view called inTextView but inside the method you are referring to myTextView and you don't mention where that got set.
I would suggest that you change all the myTextView references to inTextView and see if that resolves the problem as you may be working on a UITextView other than myTextView and seeing your problems because of that.
精彩评论