UITextViewDelegate not called - objective-c

My UITextView delegate is not getting called. I set the delegate of the textview to self in cell for row by saying cell.textview.delegate=self. (The cell is a custom subclassed cell with UITextView property.) Nevertheless, in my tableViewController the following line does not get called:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Why?

Related

Hiding Keyboard on UITextView

I'd like to thanks you in advance for your answers. I read all stuff about this but I couldn't solve it.
I know how to hide keyboard on UITextField, just like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[placename resignFirstResponder];
[address resignFirstResponder];
return YES;
}
But I also have an UITextView and I don't know how to hide it. I don't want to hide it with a button I want to hide it with a "Done" in the return.
I read something like this:
[textField setReturnKeyType: UIReturnKeyDone];
But... how and where to implement it? I'm new on this so I need to know it step by step,please.
Thanks so much.
(Sorry I'm not able to send an image...new user ;) )
Its easy. Have textview delegate in your controller and add following method.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
There is one disadvantage using this, user can not write multi line statements. To solve this you can have a create view over keyboard and have button over there and add action to resign your textview.
Credit to this question
Additional to #JanakNirmal Answers, the code for the .h and .m. Works fine for me.
.h
#interface ViewController : UIViewController <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextView *textField; // connected with IB
#end
.m
#interface ViewController ()
- (void)viewDidLoad
{
[super viewDidLoad];
// Done key and hide keyboard
[self.textField setDelegate:self];
[self.textField setReturnKeyType:UIReturnKeyDone];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
}
return YES;
}
#end

UITextField not calling delegate method

I have a DetailViewController, which implementation file contains this code:
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController <UITextFieldDelegate>
{
__weak IBOutlet UITextField *nameField;
__weak IBOutlet UITextField *numberField;
}
#end
In my storyboard, I have set the ViewController's to DetailViewController and connected the delegate of both of my UITextFields to my DetailViewController. The implementation file of my DetailViewController contains this method to dismiss the keyboard when tapping somewhere other than the text field:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
This method is not called though, I have tested this using a breakpoint. What could be going wrong?
rdelmar is correct, the code you have only gets triggered when the user hits the "return" key on the keyboard, not when they click outside of the keyboard.
To get the behavior you are looking for, I'd add a Tap Gesture Recognizer to the view behind your text field, then put [nameField resignFirstResponder]; and [numberField resignFirstResponder]; in the tap gesture recognizer's code.
I created a sample project
added to the viewcontroller
added a textfield and connected its delegate
write the code in viewcontroller
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
it worked fine.Tried weak it also worked both as instance variable and property
So check if that you are reallocating it anywhere and also check its memory is same there using breakpoint
Why do you use _weak? Remove it.
In your .m file do (in viewDidLoad):
[nameField setDelegate:self];
Same for the other textfield(s).

UITextField and #property id<UITextFieldDelegate>

I have UITextField and I have
#property (nonatomic, assign) id <UITextFieldDelegate> delegate;
After pressing a done button on keybord, it should dismiss. Can someone tell how I can do it using this property?
The dismissing of the keyboard does not happen merely by being a delegate of the UITextField. Your delegate must dismiss it.
In the viewDidLoad in your controller you must assign the delegate, or set it up in Interface Builder:
- (void)viewDidLoad {
self.textField.delegate = self;
}
Then in your controller, implement the following delegate method.
#pragma mark - UITextFieldDelegate
// This method gets called when you hit the enter key on the keyboard,
// or in this case 'DONE'. The textfield is asking if it should put
// a carriage return in the field. This is our opportunity to dismiss
// the keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder]; // This is what hides the keyboard
return NO;
}
Further reading:
Responder Chain - developer.apple.com
UITextFieldDelegate Protocol Reference - developer.apple.com
Link didEndOnExit to a method that has [self resignFirstResponder]

UITextView shouldChangeTextInRange delegate not called

I am using this code to set the parameters of an uitextview i have on my view.
_textview=[[UITextView alloc]init];
[_textview setUserInteractionEnabled:FALSE];
_textview.text=#"Write your message";
_textview.textColor=[UIColor lightGrayColor];
_textview.layer.cornerRadius=6;
_textview.contentInset = UIEdgeInsetsZero;
_textview.delegate=self;
I have in the .h this code
IBOutlet UITextView *_textview;
#property(nonatomic,retain) IBOutlet UITextView *_textview;
I have connected the outlet to the uitextview by using the interface.
What happens is the following:
- (void)textViewDidChange:(UITextView *)inTextView
the above delegate is called but not the following one:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementString:(NSString *)string
Why is this happening? Am i doing something wrong?
Any help appreciated...
In addition to removing the _textview = [[UITextView alloc]init]; so as to not overwrite your nib loaded textview.
The <UITextViewDelegate> method is:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{}
The correct method signature is:
textView:shouldChangeTextInRange:replacementText:
not:
textView:shouldChangeTextInRange:replacementString:
Because you are typing the method name wrong.
There is no
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementString:(NSString *)string
but only
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
careful with the different between replacementString and replacementText.
Also, as the others said, if you've created the UITextView in IB, you should NOT init it again in your code (but this is not the reason why your delegate never invoked).

Programmatically dismissing textfield keyboard with delegate

I have a viewController with this:
#interface RootViewController : UIViewController <UITextFieldDelegate>{
Then I have a textfield added programmatically like this in viewController.m:
UITextField*textFieldRounded=[[UITextField alloc] initWithFrame:frame];
textFieldRounded.delegate = self;
In the same .m i have tried all of these:
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(#"returned?");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(#"returned?");
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(#"returned?");
return YES;
}
The keyboard appears fine when I click the text field, which is in a subview of the viewController. but pressing Done does nothing. The NSLog statements never appear, so these methods are never getting called. Any ideas?
I just barely had this question, maybe, but what I did was attached a tap gesture recognizer object to the view, made a instance method called clear keyboard:, and inside the function I put [whateverItIsThatOpenedTheKeyboard resignFirstResponder]; and that's it. Maybe you asking something totally different, but I thought I would try and help :)
Declare the UITextField in .h file as an IBOutlet with property and synthesize it in .m file.
Use this delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
Add this delegate method to your code and run your app once again..