I want to hide the View after clear the Text field data.But My view is not hiding.Please give me the solution i am using this code
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
partialSearchView.hidden = YES;
isCheckType = YES;
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
partialSearchView.hidden = NO;
isCheckType = NO;
return YES;
}
you should use the setter method: ...
[self.partialSearchView setHidden:YES];
- (BOOL)textFieldShouldClear:(UITextField *)textField
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.
Implementation of this method by the delegate is optional. If it is not present, the text is cleared as if this method had returned YES.
you need actually implementation
- (BOOL)textFieldShouldClear:(UITextField *)textField {
partialSearchView.hidden = YES;
isCheckType = YES;
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (isCheckType )
return YES;
partialSearchView.hidden = NO;
isCheckType = NO;
if (textField.text.length >=8) {
return NO; // return NO to not change text
} else {
return YES;
}
}
Related
I am facing the the wierdest bug (ether in my app or in IOS 7.1) ever.
After many hours I managed to create a simple app that demonstrate the problem.
Two UITextField - Dragged and dropped from interface builder and wired to t1, t2.
the ViewController:
#implementation ViewController
#synthesize t1;
#synthesize t2;
#pragma mark - UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)iTextField {
NSLog(#"textFieldDidBeginEditing");
[iTextField performSelector:#selector(selectAll:) withObject:iTextField afterDelay:0.0];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
t1.delegate = self;
t2.delegate = self;
}
#end
When tapping on t1 and t2 at the same time, both textFields become first responder in an endless loop!
When omitting ether the PerformSelector statement or the textField:shouldChangeCharactersInRange: implementation, problem is gone.
Can someone explain why does it happen?
Edit: Also set the exclusiveTouch property of each UITextField to: YES to prevent them from editing at the same time.
- (void)viewDidLoad
{
[super viewDidLoad];
t1.exclusiveTouch = YES;
t2.exclusiveTouch = YES;
t1.delegate = self;
t2.delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)iTextField
{
[iTextField performSelector:#selector(selectAll:) withObject:nil afterDelay:0.0];
}
Or more simply without using the exclusiveTouch properties:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)iTextField
{
if (iTextField == t1 && t2.isFirstResponder == NO)
{
return YES;
}
else if (iTextField == t2 && t1.isFirstResponder == NO)
{
return YES;
}
return NO;
}
I try to use the selectedTextRange property to instead of selectAll, it makes the endless loop's problem gone.
func textFieldDidBeginEditing(_ textField: UITextField) {
DispatchQueue.main.async {
let begin = textField.beginningOfDocument
let end = textField.endOfDocument
textField.selectedTextRange = textField.textRange(from: begin, to: end)
}
}
I have in my TableViewCell 2 TextFields.
For the 2nd TextField i need a confirm to enable a button.
I´m using this code to determine if the confirm button should be enabled, but it doesn't work:
-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textField = self.installDeviceCell.passWordTextFieldOutlet;
[textField addTarget:selfaction:#selector(checkTextField:)forControlEvents:UIControlEventEditingChanged];
return YES;
}
-(void)checkTextField:(id)sender
{
UITextField* textField = (UITextField*)sender;
if ([textField.text length] < 7) {
self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = NO;
}else if ([textField.text length] > 7){
self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = YES;
}else{
self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = YES;
}
}
Have you assigned your UITextFieldDelegate for your UITextField when it is being instatiated? If don't assign the it the delegate methods for the UITextField will not be called.
Also make sure that in the header file of the delegate you specify the delegate of the interface e.g.
#interface MyDelegate <UITextFieldDelegate>
// ...
#end
Finally you're doing your text calculations a little oddly. You can do all your computations witin shouldChangeCharactersInRange. You also don't need to add the target or overwrite the textField property. Simply check if the replacementString is greater than 7 characters. Your final code might look something like this:
-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string length] < 7) {
self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = NO;
} else {
self.installDeviceCell.installDeviceTouchUpInsideButton.enabled = YES;
}
return YES;
}
See this great answer on delegation for more info: https://stackoverflow.com/a/626946/740474
This question already has answers here:
How to navigate through textfields (Next / Done Buttons)
(35 answers)
Closed 9 years ago.
i have 4 text fields , i want to parform following actions by clicking on return key -
1- when i enter entry in one field and press return key , curser should be jumped on next
TextField .
Thanks .
Implement UITextField delegate and set tag value to all your textField in series. like 1,2,3..as so on. then, make sure scrollView and textField tag values should not conflict.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
int nextTextFieldTag = textField.tag+1;
UITextField *textFieldd = (UITextField*)[self.view viewWithTag:nextTextFieldTag];
if (textFieldd!=nil) {
if ([textField isKindOfClass:[UITextField class]]) {
[textFieldd becomeFirstResponder];
return NO;
}
}
return YES;
}
Try this
Take the 4 textfields as textfield1,textfield2,textfile3,textfield4 .
In the textfield delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if([textField isEqual:textfield1])
{
[textfield2 becomeFirstResponder];
return NO;
}
else if([textField isEqual:textfield2])
{
[textfield3 becomeFirstResponder];
return NO;
}
// Do for other textfields
return YES;
}
You just need to know what the next TextField should be and have UITextFieldDelegateProtocol especially the - (BOOL)textFieldShouldReturn:(UITextField *)textField implemented.
In - (BOOL)textFieldShouldReturn:(UITextField *)textField simply figure out the next TextField and do [nextTextField becomeFirstResponder];
So the - (BOOL)textFieldShouldReturn:(UITextField *)textField Method could look like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:firstTextField]) {
[secondTextField becomeFirstResponder];
}
//When last textfield dismiss the keyboard
else if ([textField lastTextField]) {
[textField resignFirstResponder];
}
return NO;
}
i have 6 textfields named digit1,digit2..etc upto digit6 added as a subview over a view. i want the digit2 textfield to autofocus once the user enters a digit in the digit1 textfield and similarly digit3 should autofocus when a digit is entered in digit2 textfield.Below shown is the code i tried.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length>=1)
{
[textField resignFirstResponder];
UITextField *_textField=(UITextField*) [self.view viewWithTag:textField.tag+1];
[_textField becomeFirstResponder];
}
return TRUE;
}
What happens here is when i enter a digit in digit1 it dosent appear in digit 1 but it appears in digit2 and also when i click delete button the control is transfered to the subsequent textfields rather than deleting the text in the current textfield.Please help me to fix this.
lets assume that you have 3 textfields with names such as t1, t2 and t3
set tag value for t1 = 1, t2 = 3 and t3 = 3
set tag values for all the textfields and the write this code
then write this code
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
switch(textField.tag){
case 1:{
if(textField.text.length>=1)
{
[t1 resignFirstResponder];
[t2 becomeFirstResponder];
break;
}
}
case 2:{
if(textField.text.length>=1)
{
[t2 resignFirstResponder];
[t3 becomeFirstResponder];
break;
}
}
case 3:{
if(textField.text.length>=1)
{
[t3 resignFirstResponder];
break;
}
}
}
}
return TRUE;
}
I think the problem is that shouldChangeCharactersInRange gets called before the edit actually occurs. Since you are changing the first responder in there, the edit takes effect in the new first responder instead of the old one.
You could try moving the code inside your if block to another method, and calling it with [self performselector:withObject:afterDelay], setting the delay to some small value. Something like:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length>=1)
{
[self performSelector:#selector(switchTextField) withObject:nil afterDelay:0.1];
}
return TRUE;
}
-(void)switchTextField
{
[textField resignFirstResponder];
UITextField *_textField=(UITextField*) [self.view viewWithTag:textField.tag+1];
[_textField becomeFirstResponder];
}
That way shouldChangeCharactersInRange returns true immediately (as it should), the edit will take place in the currently selected textField, and then your switchTextField method will get called soon after. It's not the cleanest solution, but it's the quickest I can think of off the top of my head.
I found the solution
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *str = [textField.text stringByReplacingCharactersInRange:range withString:string];
if( [str length] > 0 ){
textField.text = string;
UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
if (nextResponder) {
[nextResponder becomeFirstResponder];
}
return NO;
}
return YES;
}
I am having some problems with enabling a button when you write something in a TextField.
I got it to disable if a there's nothing in the TextField but I can't make it to enable again when you write something in it.
I have something like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSUInteger textLength = [_Name.text length];
[_doneButton setEnabled:(textLength > 0)];
}
Set delegate to the UITextField and create a method for text change in ViewDidLoad as follows.
[self.textFieldTemp addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
And add the method as follows.
- (void)textFieldDidChange:(UITextField *)textField {
if(textField == self.textFieldTemp)
self.buttonTemp.enabled = ([self.textFieldTemp.text length] > 0) ? YES : NO; }
Try this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([textField.text length]>0){
[doneButton setEnabled:YES];
}
else{
[doneButton setEnabled:NO ;
}
Hope this May be help you..
Use UITextField delegate method to enable your button again. When your textField characters are changed, delegate method will be called, enable your button here.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
_doneButton.enabled = YES;
}
Put the lines in textfield delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
It'll work then. This method is called every time you input a character in textfield. But ViewDidLoad is called only when the viewcontroller is loaded.
Hope it will work for you.
These two functions will enable and disable your button according the text in your textfield
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
_doneButton.enabled = YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if([textField.text length]>0){
[doneButton setEnabled:YES];
}
else{
[doneButton setEnabled:NO ;
}
You can use delegates of UITextField to enable and disable the button like this.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
{
if (textField.text.length!=0)
{
yourButton.enabled = YES;
}
else
{
yourButton.enabled = NO;
}
}
The method I used didn't use the delegates at all. I created this method in the controller class:
- (IBAction)textFieldChanged:(id)sender
{
self.doneButton.enabled = ([self.Name.text length] > 0) ? YES : NO;
}
Then, in the Interface Builder, I set the text field "Editing Changed" action to target the new method. Then, similar to #Paramasivan Samuttiram, I added an extra call to it in the viewDidLoad:.
[self textFieldChanged:self.Name];
That part allows the button to be in the correct state initially.