I have implemented simply a uitextview, when I dismiss it with done button. It got crashed in iOS 6 with error -[UITextView setSelectable:]: unrecognized selector sent
but it is working fine in iOS 7.
-(BOOL)textViewShouldEndEditing:(UITextView *)textView1
{
[textView resignFirstResponder];
}
I am really unable to find the issue. Please help me out if someone has idea regarding this issue.
Thanks in advance.
You must have return type like this..
-(BOOL)textViewShouldEndEditing:(UITextView *)textView1
{
[textView1 resignFirstResponder];
return YES; // put this line in your code.......
}
In UITextView.h:
#property(nonatomic,getter=isSelectable) BOOL selectable NS_AVAILABLE_IOS(7_0);
this property is only available in iOS 7
Please have recheck again with Following code:
-(BOOL)textViewShouldEndEditing:(UITextView *)textView1 {
[textView1 resignFirstResponder];
}
Use this delegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
Remove the statement textView.selectable, it works without it on both versions
Related
I created a Text Field in Interface Builder. I set it's "Return Key" to Done. This is a one line only input (so it wont need multiple lines).
How do I hide the virtual keyboard when the user taps the done button?
Implement the delegate method UITextFieldDelegate, then:
- (void)viewDidLoad {
[super viewDidLoad];
self.yourIBtextField.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
UITextView does not have any methods which will be called when the user hits the return key.
Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.
There might be other ways but I am not aware of any.
Make sure you declare support for the UITextViewDelegate protocol.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
Make category on UIViewController with next method
- (void)hideKeyboard
{
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
}
In my iPad app I have two textfields. One displays the normal, default textfield and the other should display a picker as its input view.
Problem is, once I use the txt1 which displays default keyboard and then when I touch the 2nd textField the txt1 keyboard is staying visible.
I have also written [txt1 resignFirstResponder]; [txt2 resignFirstResponder]; while displaying the picker.
I have checked the txt1 IBOutlet connection and the delegate assignment, those seem to be correct.
What am I missing?
Write the following code :
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == txt1)
{
return YES;
}
else
{
return NO; // Write the code for displaying UIPickerView instead of the Keyboard.
}
}
Hope this might solve your issue......
txt2.userInteractionEnabled = NO;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == txt1)
{
[txt2 resignFirstResponder];
// code for Hide Picker
return YES;
}
else {
// [txt2 resignFirstResponder];
[txt1 resignFirstResponder];
// code for go in picker
return YES;
}
}
for more information
You have to implement below method to resign Keyboard......
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Have u implemented this method ??
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
In your viewDidLoad method write this,
txt2.inputView = pickerView;
and other resignFirstResponder codes should be placed correctly , by this on Tapping txt2, you will directly get pickerview instead of Keyboard.
did you implement the delegates property of UITextFieldDelegates to in the header file, if not do that and check
I have a UITextView. When the user hits the Send key I want to be able to automatically perform a selector. How can I do this? I know UITextField has
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Try this out:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text; {
// Any new character added is passed in as the "text" parameter.
if ([text isEqualToString:#"\n"]) {
// If the Done button was pressed, resign the keyboard.
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added.
return NO;
}
// For any other character return TRUE so that the text gets added to the view.
return YES;
}
Hope that Helps!
you can use
[self performSelector:#selector(aMethod) withObject:nil afterDelay:0.1];
in
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
return YES;
}
Hope it helps you..
I'm getting problem in resigning the keyboard after clicking done button. I'm using textView
-(BOOL)textViewShouldReturn:(UITextView *)textView
{
if(textView == addressView)
{
if(isNotif)
{
[self setViewMovedUp:NO];
}
textView.text= [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[addressView resignFirstResponder];
}
return YES;
}
Instead of keyboard resigning the cursor is coming to new line in the text field.
Please help me.
Thank You
Praveena.
Fixed KingOfBliss's answer:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == messageInput) {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
}
return YES;
}
Use the following delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == YourTextField){
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
}
return YES;
}
Usually in textView the return key is use to add \n to the text, so its better to add some other button to top of the UItextView and code the resigning function there.
EDIT:
There is no such delegate -(BOOL)textViewShouldReturn:(UITextView *)textView
this will happen if the ViewController that is above this textView in the view hierarchy is not the delegate of this textView. If it is not then the ViewController will never get the message textViewShouldReturn. In the viewController after the subView (the UITextView) is created.
aTextView.delegate = self;
To check to make sure it is getting called add this to your function and test
NSLog(#"resigning first responder");
this will test to see if this function is even getting called
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
was enough in my case.
How can I set editing= NO; in UITextField ?
You can set it like
[yourTextField setUserInteractionEnabled:NO];
Swift Update:
yourTextField.setUserInteractionEnabled = false
Also you can implement the protocol and implements this method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
Doing this any UITextField in associated XIB will be editable, but you gain the hability for checking any properties or doing some business logic inside for enable/disable text editing.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (condition)
return NO;
else
return YES;
}
I hope this helps you
Regards!
If you mean you want to hide the UITextField, just do:
[UITextField resignFirstResponder];
Use this piece of code
[textField setEditable:NO];