How to compare the instance of UITextField - iphone

I have 3 UITextFileds on a view, and I want to apply logic in UITextFieldDelegate method only in two of them, how do I determine the UITextField that triggered the call backs?
Many thanks in Advance!

Usually, simple pointer comparison works, since you just want to check for object identity.
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
if (textField != theIgnoredTextField) {
...
Alternatively, you could assign .tags to the text field.
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
if (textField.tag != 37) {
...
The advantage is you don't need to store the reference to theIgnoredTextField, and the tag can be set from Interface Builder, but it relies on a recognizing magic number "37".

The delegate methods have a textfield parameter which is a point to the textfield object. You can compare that parameter to your textfield objects to see which one it is.
UITextField *field1, *field2, *field3;
In your delegate method you can compare the parameter:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == field1) {
// do something special for field 1
} ...

if you are using the delegate methods, such as - (void)textFieldDidEndEditing:(UITextField *)textField all you need to do is do something like
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == myFirstTextField)
{
//blah
}
else if (textField == mySecondTextField)
{
//etc etc.
}
else
{
//WHEE!
}
}//method end

You can compare the pointers if you keep references to them in your class ivars or you can use UIView's tag property, whichever you like more.

Related

Keypressed event in UITextField

I have 10 textfields, in which I could enter only one character in each textfield. After a character is entered in each textfield, the focus should move to the next one. Similarly when i delete character from a textfield by pressing the backspace or delete, i need to get the focus to the previous textfield. If I could get the keypressed event, I could do that. Right now I am not able to find any keypressed event examples.
Implement UITextFieldDelegate.
Implement the delegate methods in the protocol. You can achieve the things you wanted.
You can set the focus by using the method becomeFirstResponder to the required textfield.
Have a look at the delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
So that could solve your problem.
Based on Aadhira's answer, but taking into account Kirk Woll's comment, you can generate what the latest text will be by using stringByReplacingCharactersInRange:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"value: %#", value);
return YES;
}
Just to give you directions:
Assign tag to each text field.
Implement UITextFieldDelegate. There are all the methods you need to detect any event that takes place inside the text field. In each method you can check the tag and move focus properly.
Hint: you can use [mainView viewWithTag:XX] to quickly pick the text field you need.
Each time the text is changed you can check the text property of the text field and it will give you the answer which button was pressed.
you have to implement the UITextFieldDelegate protocol into your code and this method will tell you when you start begin editing in text field
– textFieldShouldBeginEditing:
and you can set the if condition in this method according to your requirement...
You have to use the textfieldDelegate methods.
In your textFieldShouldReturn method you have to set your responders like
if (textfield == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else
{
[textField3 resignFirstResponder];
}
return YES; // as method return type is BOOL.

how to clear the text field by using the delegate functions

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length >=8)
{
return NO; // return NO to not change text
}
else {
return YES;
}
}
when i am adding this method to my program, text will not be clear.
how can i clear my text field. by using the below method
- (BOOL)textFieldShouldClear:(UITextField *)textField
The behavior you are seeing does not depend on textFieldShouldClear:, whose default implementation already returns YES (source):
The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES.
The problem lays with textField:shouldChangeCharactersInRange: denying any change whenever the textField contains more that 8 characters:
if (textField.text.length >=8) {
return NO; // return NO to not change text
I don't know why you set this or if you could find another way to get the same, but if you want to leave it like this, then a possible workaround is checking the replacementString and if it is empty, allow the text change by returning YES.
If you want a more sophisticated solution, you could think of setting an ivar flag when textFieldShouldClear: is called, so that when you find the flag set in textField:shouldChangeCharactersInRange:, you return YES.
- (BOOL)textFieldShouldClear:(UITextField *)textField {
self.shouldClearTextCalled = YES;
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (self.shouldClearTextCalled)
return YES;
self.shouldClearTextCalled = NO;
if (textField.text.length >=8) {
return NO; // return NO to not change text
} else {
return YES;
}
}

textFieldDidBeginEditing: for more than one textfield

I am fairly new to iphone programming and here I am facing some issues. Now in my application, I have two textfields and I want to fire an event while second textfield starts editing. now I am using following function
- (void)textFieldDidBeginEditing:(UITextField *)textField
but the thing is the event is being fired when the first textfield starts editing. It does not wait for the second text field. Is there any way I can use this function for the second textfield or may be somehow could know and pass it the value of the active textfield?
I tried writing the name of the textfield instead of (UITextField *)textField in the function but still the same result.
If I were you , I would set a tag (in Interface Builder) of the second textField to 2, or something similar. Then you can just do this:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag == 2) {
//this is textfield 2, so call your method here
}
}
EDIT: Please do this to see if the method is even called:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"The method was called");
}
For Swift 2.2
func textFieldDidBeginEditing(textField: UITextField) {
if textField.tag == 2 {
//this is textfield 2, so call your method here
}
}
That delegate method is gonna get called everytime the editing of ANY text field is started, so it should be you who controls what is done when this happens. I suggest you to do something like:
-(void)textFieldDidBeginEditing: (UITextField *)textField
{
if (textField == mySecondTextField)
{
//Do what you need
}
else
{
//Do nothing
}
}
I hope it helps you!
Utilitize the tag property in Interface Builder to identify your view objects in your application at runtime. It will make life a lot easier, especially when you get ready to localize your application for different languages.
In your header file for your view controller
#define kUsernameField 100
#define kPasswordField 101
#define kStartButton 300
In the view controller implementation file
- (void)textFieldDidBeginEditing:(UITextField *)textField {
switch (textField.tag) {
case kUsernameField:
// do user name stuff
break;
case kPasswordField:
// do password stuff
break;
default:
NSLog(#"No case statement for %#", [textField description]);
break;
}
}
You will find a lot of tutorial out there that use the title field of UIButton to identify them. For example:
- (IBAction)buttonTouchUp:(id)sender {
UIButton *button = (UIButton *)sender;
// don't like
if ([button.currentTitle isEqualToString:#"Start"] == NSOrderedSame) {
// because if localize your for other language then you will have
// include code for those other language
// French: Démarrer
// Spanish: Inicio
// blah blah blah
}
// better
if (button.tag == kStartButton) {
// very simple, no code changes for localization
// blah blah blah
}
}
If you are creating the object with code, you can set the tag:
button.tag = kStartButton;
// or
[button setTag:kStartButton];
You must declare first UITextFieldDelegate in your controller .h
And set the delegate of your text field. ex. myInput.delegate = self;
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:myInput])
{
NSLog(#"test");
}
}
This works perfectly for me.
Have you checked if your second textViews delegate is set to self ? I had the same issue where I had forgotten to set the delegate of other textFields and hence the delegate method was not firing.
Please have a look to my answer in this Question, it's exactly what you're looking for
Objective C: what is a "(id) sender"?

iphone textfield focus to send field

hello all i have my four textfield for entering password If my password is "1 2 3 4", is there a way to automatically jump to the second box as soon as I enter "1" and so on and so forth?
use a UITextViewDelegate to detect the input and then call becomeFirstResponder on the next text field.
you need to make your class conform to UITesxtFieldDelegate and then use delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(texField == textfield1)
{
if([textfield1.text isEqualToString: #"1"])
{
[textfield1 resignFirstResponder];
[textfield2 becomeFirstResponder];
}
}
else if(texField == textfield2)
...... // write similar code
}
Text field calls this method whenever user enters some text or delete text from textfield. Also you need to set in your viewdidload method textfield1.delegate=self; for each textfield you need to call this method. For further details read doc for UITextFieldDelegate.

delete last character UITextField

I have an UITextField and I would like that for every tap on a character, the first character is deleted. So that I just have one character in my textField every time. Moreover I would like it to display every tap in the console log.
How can I do this?
You need to implement shouldChangeCharactersInRange method in your text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string{
textField.text = #"";
return YES;
}
You may need to check for range and string values to cover all possible cases (like copy/paste actions). This code just sets the text field's value to the last typed character.
UITextField inherits from UIControl, so you can use the target-action mechanism that is part of the UIControl class:
[textField addTarget:self action:#selector(updateTextField) forControlEvents:UIControlEventValueChanged];
In the action method, you can replace the UITextField's text with only the last character and log that character in the console. Note that since changing the UITextField's text will again result in the "updateTextField" message being sent a second time to the target, you will need some kind of mechanism for determining whether to update or not:
- (void)updateTextField {
if(updateTextField == YES) {
updateTextField = NO;
NSString *lastChar = [textField.text substringFromIndex:[textField.text length]];
[textField setText:lastChar];
NSLog(#"%#", lastChar);
} else {
updateTextField = YES;
}
}
Or something like that anyway...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length > 8) {
return NO;
}
return YES;
}