how to clear the text field by using the delegate functions - iphone

- (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;
}
}

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.

Cococa Touch - Verifying each character inputed

I want to have the user type in something. And I want to verify each character inputed. How could I do this? I want it to be in real time. So as its typed its being verified and color coded.
You could compare what I want to do to an programmers IDE how it checks the syntax as you type it.
Any help is appreciated!
Connect the UITextField delegate to the app delegate and do something like:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([string isEqual:#"b"]) {
// User typed 'b'
// Insert string with specific color
// [...]
} else {
// User typed something else
// Insert string without specific color
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
}
return YES;
}

How to compare the instance of UITextField

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.

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;
}

Solution for iPhone new file dialog keyboard

i want to let the user type in the name of a new file, so there are certain characters i want to prevent entry on. is there a special keyboard i can use or can i disable certain keys on the iphones keyboard.
is the answer to just run a regular expression on the input text and tell the user the filename is invalid (if so what would that regular expression be?)
ANSWER: (or what i ended up doing)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL valid;
//if the user has put in a space at the beginning
if ([string isEqualToString:#" "]){
if (range.location == 0){
valid = NO;
}
else{
valid = YES;
}
}
//otherwise test for alpha numeric
else{
NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
valid = [[string stringByTrimmingCharactersInSet:alphaSet] isEqualToString:#""];
}
//print the warning label
if (valid == NO){
[errorLabel setText:#"Invalid input"];
}
else{
[errorLabel setText:nil];
}
return valid;
}
You can implement the delegate method
For UITextField,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
For UITextview
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
and decide weather to append the entered characters or not.
You can implement the UITextFieldDelegate protocol and use textField:shouldChangeCharactersInRange:replacementString: to watch the text entry and prevent unwanted characters by returning NO.