I have multiple uitextfields but they all respond to the delegate the method. I tried using textfield description but that doesn't work. There must be a way to know which text field is active but I can't find it.
thanks.
According to the UITextFieldDelegate protocol reference, all the methods pass the UITextField that's calling the method. For example:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
You can just check the text field against a known one:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == self.fooBarField)
{
//do magic
}
return YES;
}
Related
Im trying to implement a delegate for a NSTextField object so I can detect the user input in real time and give some feedback about no allowed input in that particular field.
Especially, I want to simulate the onChange() method from JavaScript, detecting the user input in real time and show him a warning if it is writing a non supported value.
i.e. The app have a text field it only accept numeric values from 0 to 255 (like RGB values) and I want to know when the user is writing not numeric values or out of range values to instantly show him a warning message or change the text field background color, just a visual hint to let him know the input it's wrong.
Like you see on the pictures above, I want to show a warning sign every time the user inputs a forbidden value in the text field.
I have been reading a lot of the Apple's documentation but I don't understand which delegate to implement (NSTextFieldDelegate, NSTextDelegate, or NSTextViewDelegate), also, I have no idea how to implement it in my AppDelegate.m file and which method use and how to get the notification of user editing.
Right now, I already set the Delegate in my init method with something like this [self.textField setDelegate:self]; but I don't understand how to use it or which method implements.
I found a solution using the information posted in this question... Listen to a value change of my text field
First of all I have to declare the NSTextFieldDelegate in the AppDelegate.h file
#interface AppDelegate : NSObject <NSApplicationDelegate, NSTextFieldDelegate>
After that, I have to instantiate the delegate for the NSTextField object I want to modify while the user update it in the AppDelegate.m file.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self.textField setDelegate:self];
}
Finally, I implement the methods to detect field editing with the changes I want to set.
- (void)controlTextDidChange:(NSNotification *)notification {
NSTextField *textField = [notification object];
if ([textField doubleValue] < 0 | [textField doubleValue] > 255) {
textField.textColor = [NSColor redColor];
}
}
- (void)controlTextDidEndEditing:(NSNotification *)notification {
NSTextField *textField = [notification object];
if ([textField resignFirstResponder]) {
textField.textColor = [NSColor blackColor];
}
}
Make your class conform to the NSTextFieldDelegate protocol. It need's to be that protocol because in the documentation it says the type of protocol the delegate conforms to.
#interface MyClass : NSObject
And implement the delegate's methods (just add them to your code). Example
- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
{
}
EDIT:
I think in your case it would be better to replace the TextField for a TextView and use a NSTextViewDelegate, in the delegate, the method of most interst of you should be
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
BOOL isValid = ... // Check here if replacementString is valid (only digits, ...)
return isValid; // If you return false, the user edition is cancelled
}
So i was reading the description about UITextView's and it says that it automatically hides the keyboard when you press the 'Return' button on the keyboard. But it wasn't working, so I tried creating an
- (IBAction)textViewReturn:(id)sender;
{
[myTextView resignFirstResponder];
}
That did not work either so i tried also doing:
- (BOOL)textViewShouldReturn:(UITextView *)textView
{
[myTextView resignFirstResponder];
return NO;
}
Not sure why the whole deal isn't working in the first place. Wondering if anyone could help?
I don't see anything in the UITextView Class Reference that says it automatically hides the keyboard when you press Return.
Also, there is no textViewShouldReturn: message in the UITextViewDelegate protocol. There is a textFieldShouldReturn: message in the UITextFieldDelegate protocol, but a text view is not a text field.
If you want it to hide the keyboard when the user presses Return, you need to do two things.
First, you need to connect some object - usually your view controller - to the text view's delegate outlet. You can do that in your nib, or you can do it in code, perhaps in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
myTextView.delegate = self;
}
Second, you need to implement the textView:shouldChangeTextInRange:replacementText: in your delegate object:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Note that if the user pastes in text containing a newline and other characters, this will not catch the newline. It will only notice when the user either taps the Return key, or when he pastes in text containing just a newline.
You can declare the delegate's class as conforming to the UITextViewDelegate protocol, in which case Xcode will helpfully autocomplete the method name. But it will work even if the class doesn't conform to the protocol.
Return button of UITextView is used to mote the cursor to the new line. But if you want to remove the keyboard on return button then please try the following code. It resigns the keyboard when return button is pressed by user. So try following code which definitely solved your problem.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
} else {
return YES;
}
}
Per your comment, if you want to use a UITextField instead of a UITextView, then things remain the same except that in order to hide the keyboard when you hit return, you need to implement the following function in the text field's delegate (make sure that you have set this first):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Hi i am new to the iOS,
i am using several text fields in my project, so i want use becomeFirstResponder for editing done for any one of text field. it is working fine in xcode 3.2.5 but it makes problem in xcode 4.2.1
In xcode 4.2.1 after calling the becomeFirstResponder i could not edit the text field, it is showing the virtual key board i could tap the keys but tapped keys are not entered in text field.
Please help me out.
Firstly add this delegates in interface:
#interface yourViewController : UIViewController<UITextFieldDelegate>
If using from xib then right click on UITextField and bind delegate with fileowner else
add this code in viewDidLoad's method.
yourTextField.delegate = self;
Add UITextField's delegate and it will be called
- (BOOL)textFieldShouldReturn:(UITextField *)textField //resign first responder for textfield
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return YES;
}
I have some textfields and I want to do when I change textfield1 text set text to other textfields. My code below. But it not works. How can I solve this?
- (IBAction)TCKimlikTextChange:(id)sender {
[TCKimlikText addTarget:self action:#selector(yourMethod: ) forControlEvents:UIControlEventEditingChanged];
}
-(void)yourMethod: (UITextField*)tf_{
if (tf_) {
if (TCKimlikText.text == #"1") {
AdinizText.text = #"Hacer";
}
}
}
Your code is very abstract. yourMethod, tf_ TCKimlikTextChange are all expressions that are not very human readable. You should work on your variable names.
I suppose your first method is a button handler. It just assigned a target and action to the text field, but does not call any method. You do not need that action if you use the delegate protocol.
To solve your problem: implement the UITextField delegate methods. Make sure you set the delegate (probably self) for your text fields. Your view controller must mention the <UITextFieldDelegate> protocol in its .h file. Thus, in textField:shouldChangeCharactersInRange:replacementString::
if ([textField.text isEqualToString:#"1"]) {
displayLabel.text = #"Hacer";
}
Notice that you need isEqualToString: to compare strings, a simple == won't do.
If u are want to change on the click of the return button use the delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == field1)
[field2 setText:field1.text];
[field1 resignFirstResponder];
return YES;
}
or u can use other delegates too like:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
I have two textfield, in first textfield I write "Hello" and when I push enter in iPad keyboard, I want that in second textfield appear "World"; How can I use enter to create an action in my application?
You would typically assign your view controller as the text field's delegate and then implement the textFieldShouldReturn: method, e.g.:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
otherTextField.text = #"World"
return YES;
}
You can do that by implementing the UITextFieldDelegate protocol in your controller. For instance you could do something like:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == theFirstTextField && [textField.text isEqualToString:#"Hello"]) {
theSecondTextField.text = #"World";
}
return YES;
}
Set your view controller to be the textfield's delegate then implement
-(BOOL)textFieldShouldReturn:(UITextField *)textField
this gets called when the enter button is pushed on the keyboard.
This is roughly what you'd do. Tweaking to condition around device-type (if you truly want iPad only):
#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.firstTextField && [textField.text isEqualToString:#"Hello"]) {
self.secondTextField.text = #"World";
}
return YES;
}