I have a cell array not in a cell collection or UItable. How do I detect an edit event on one of the cells?
First of all, set tags to your all textfields. so that you can recognise current textfield.
[textfield setTag:101];
Set delegate for each textfield.
[textfield setDelegate:self];
and at last use UITextField delegate textFieldDidBeginEditing for detecting "edit"
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField.tag == 101){
//do something
}
}
Set textfield Delegate "UITextFieldDelegate" for access the textfield methods.
And impliment the "textFieldDidBeginEditing" methods for detect an edit even in textfield.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEqual:"Your TextField Outlet name"])
//Do Something you code
//Return Value (true or false)
}
For Example:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEqual:txtContactNo])
//Do Something you code
//Return Value (true or false)
return YES;
}
Related
I have added target on UITextField of UITableViewCell.I have used below code for that.
cellIdentifier = #"TextFieldCell";
TextFieldCell *txtCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
txtCell.indexPath = indexPath;
[txtCell.textFieldData addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
Now for in target method described as below.
-(void)textFieldDidChange :(UITextField *) textField{
}
I have added indexpath property in below TextCell.In callback method i want to get the refrence of textfield on which i have added target so that i can access the indexpath property.
I want to get the txtCell.textFieldData in the callback method.Please tell me how can i do this ?
add one or more line in your tableview datasource method for find the which textfield you selected or identify which textfield you tapped
txtCell.textFieldData.delegate = self;
txtCell.textFieldData.tag = indexPath.row;
txtCell.textFieldData.text = [self.yourarrayName objectAtIndex:indexPath.row];
[txtCell.textFieldData addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
on your action you can get the reference of your current textfield
-(void)textFieldDidChange :(UITextField *) textField{
NSLog(#"textField values== %%#",textField);
}
update and example
if you want to update the textfield, then follow like
-(void)textFieldDidChange :(UITextField *) textField{
NSLog(#"this is your current textfield values== %%#",textField);
[self.yourarrayName replaceObjectAtIndex:textField.tag withObject:textField.text];
if (textField.text.length > 0)
{
[textField setNeedsDisplay];
// do something whatever you need
}
}
You could sublass your UITextField to contain a property of type NSIndexPath.
#interface MRTextField : UITextField
#property (strong, nonatomic) NSIndexPath *indexPath
#end
In your storyboard/xib just select the UITextField and change its class to MRTextField. Create an outlet for this textfield in your TextFieldCell and it will be of the type MRTextField instead of UITextField. From your storyboard control + drag your text field to the UIViewController and set it as the delegate.
Then just implement the delegate method of the UITextField shouldChangeCharactersInRange
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
MRTextField *tempTextField = (MRTextField *)textField;
//access the indexPath property using tempTextField.indexPath
return YES;
}
Note: The textfield doesn't have the latest change the user performed in the above delegate method, for example if he typed in s the textfield.text will contain text before the s has been added to the string. You can either add it on your own and save to the model in the above method or perform your operations in the following delegate:
-(void)textFieldDidEndEditing:(UITextField *)textField{
MRTextField *tempTextField = (MRTextField *)textField;
//access the indexPath property using tempTextField.indexPath
}
First you have to give tag to textfield of perticular cell then in target method you can get that tag and then use following :
TextFieldCell *txtCell = [tableview cellForRowAtIndexPath:tag]
Now you can access --- txtCell.textFieldData
I have a UITableView that contains custom tableView cells. This custom UITableViewCell contains two UITextFields. I have assigned each one of the textFields a tag value, and what I would like to do is determine if both UITextFields contain text. I would like to do this as the user is entering values inside the UITextFields, such that once a user has entered text in UITextField A, and has entered a character in UITextField B or vice versa (i.e. user has entered text in UITextField B, and entered a single character in UITextField B), an event or action is triggered. I realize that I need to use the UITextFieldDelegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {}
however, my problem is that I am not sure how to reference BOTH UITextFields while using this method. I can't seem to figure out how to get the reference to the active custom UITableViewCell while I am in this method. Does anyone have any suggestions?
This is what I will do:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
UITextField *theOtherTextField = nil;
// Get a list of sibling views of the textField
for( UIView *sub in textField.superview.subviews ){
if( textField!=sub && [sub isKindOfClass:[UITextField class]] ){
theOtherTextField = (UITextField *)sub;
}
}
// Now you have 'textField' and 'theOtherTextField' ready to use
}
BTW, this is how you get a reference to the cell, but it is depending on how deep you have your textfields in the UITableViewCell's view hierarchy:
UITableViewCell *cell = (UITableViewCell *)textField.superview.superview;
And you probably set your UITableView as the UITextFieldDelegate. If you would like to change so that the UITableViewCell as the UITextFieldDelegate, you may be able to avoid most of the trouble above.
You walk up the superviews of the UITextField until you find a UITableViewCell. You then ask the tableview for the index of that cell.
In custom cell, you have two property textField1 and textField2 reference to two text field in your cell. When delegate textField:shouldChangeCharactersInRange: invoked, you will check use two above property:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (self.textField1.text.length > 0 && self.textField2.length > 0) {
// do what you want
}
}
You can also detect that property corresponse with which text field:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.textField1) {
// text field 1's text changed
}
else if (textField == self.textField2) {
// and this is text field 2
}
}
Don't use shouldChangeCharactersInRange! I see that so often and it isn't what that method is there for. That method is for stopping invalid input, for example entering letters in a number field.
Instead subclass UITableViewCell and make your custom cell. Give it a couple of IBActions and have its UITextFields connect to those actions with the EditingChanged event. Keep track of what values are in them from there. You could also give the class a couple IBOutlets if you want to peek at the values.
In your Custom Cell, since you have already tagged them, you can access the TextField with help of those tags.
Make two private instance of textFields, then inside initWithStyle, assign them with your textFields Tag.
_textField1 = [self viewWithTag:tag1];
_textField2 = [self viewWithTag:tag2];
_textField1.delegate = self;
_textField2.delegate = self;
Now inside textField Delegate method, you can check for the text length of both textFields.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(_textField1.text.length > 0 && _textField2.text.length > 0){
// write the action you want to do here
}
}
I have custom UITableViewCell with UITextField.
The problem is that i am not able to dismiss that keyboard.
In my TVC i have UITextFieldDelegate and in viewDidLoad method i implement:
customCell.textField.delegate = self;
and of course :
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[customCell.textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[customCell.textField resignFirstResponder];
} return NO;
}
i tried implement this code into my CustomTableViewCell class.
Thanks for any help.
You already set the delegate of the textfield.
-(BOOL)textFieldShouldReturn:(UITextField *)textField; takes a textField so you didnt need to recreate the cell just to access it because it is being passed to the delegate so use it directly.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return NO;// or YES depending on what you trying to do
}
In my iPad application i have few UITableView on same view created programmatically. They must pretend one multicolumn table. Each cell contains UITextField. It's size is equal to cell's size (its the reason why i cant get UITableView's delegate methods didSelect/didDeselect row). My problem is when i begin edit one text field then try to remove focus to other textfield it needs two taps. After first tap no one of cell is not editing. Such behavior observing only inside same table. If i want to change focus to other table its possible in one tap. What i missed?
Here is my UITextField Delegate methods in Cell's class
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([_delegate checkForAccess:self]) {
if (!((CTKTextField *)textField).isEditable)
{
[_delegate callPickerUnderCell:self];
return NO;
}
else
{
[_delegate getPosition:self];
return YES;
}
}
else return NO;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (_textField.text.length == -1)
{
_textField.rightView = nil;
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *input = textField.text;
[_delegate saveEdit:self withText:input];
}
If textFieldShouldBeginEditing is called after first tap and you return YES then text field starts editing. Probably you are making endEditing to the whole tableView after that.
If you call endEditing on the specific cell in your cell's delegate instead of the whole table view it should work.
That might be caused by autocapitalizationType. Your have to dismiss the auto correction before getting your textField becomeFirstResponder. Disable it will solve the problem.
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
Edit:
I was confused, try this one:
textField.autocorrectionType = UITextAutocorrectionTypeNo;
Try to set UITableViewCellSelectionStyleNone to your table view cells with text fields.
I have an application where the user selects items in a tableView and he clicks in a textField inside the row and places a number (for quantity). What I want to do is that when the user clicks the textField, the symbols and numbers keyboard would appear, or even better, a number keypad. This is what I have so far:
In my SearchResultTableViewCell.m class, the class I use for my custom tableViewCell, I have this function:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField setKeyboardType:UIKeyboardTypeNumberPad];
[textField reloadInputViews];
return YES;
}
So far, it does not work. I also tried to do
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[quantity setKeyboardType:UIKeyboardTypeNumberPad];
[quantity reloadInputViews];
return YES;
}
Where quantity is the IBOutlet UITextField as declared in the .h file.
You should declare IBOutlet of textfield in custom tableview cell's .h file & set keyboard type in
1) awakeFromNibin custom cell's .m file,
-(void)awakeFromNib
{
[self.textField setKeyboardType:UIKeyboardTypeNumberPad];
}
2) cellForRowAtIndexPath of tableview's datasource method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Init you cell as you are doing right now
CustomCell* cell;
[cell.textField setKeyboardType:UIKeyboardTypeNumberPad];
}
You're doing all this work at "textFieldShouldReturn", which is the end of entering in text into some text field (the quantity text field or some other text field).
Why not do this work in "textFieldShouldBeginEditing:(UITextField *)textField", or why not just simply set the keyboard type of the Quantity text field in the storyboard or xib file?
You are using the wrong method to set the keyboard!
textFieldShouldReturn is called when the return button on the keyboard is pressed.
You must set the keyboard type when you are initializing the textField in the code or set the property in .xib or if you want to set the keyboard type when user clicks the textField then put your code in the following method textFieldShouldBeginEditing:(UITextField *)textField