Very beginner obj-c question.
I have grouped TableView with two sections http://uaimage.com/image/c6c9ca23 . In first section I have custom cells with UITextField's in them. I need to implement Input Accessory View For keyboard with additional buttons "Next", "Prev" (to switch between text fields in first section) and "Done"(to dismiss the keyboard) http://uaimage.com/image/62f08045 .
Question is: what do I need to implement possibility to switch between text fields in cells in first section of TableView by tapping input Accessory buttons?
Do I need to tag cells or text fields and if so, how can I retrieve their value when user will tap on Input Accessory buttons? Or it is a better approach to do this?
Thanks, Alex
I assume say u have 3 UITextField ie txt ,txt1, txt 2 with tags 0 1 and 2; Now add UITableViewCell *cell in .h file.
EDIT :
Now to get references of all textField from current tableView cell add this delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
cell = nil;
cell = (UITableViewCell *)[textField superView];
return YES;
}
Now in Input Accessory prev button action do this:
-(IBAction)previousBtn:(id)sender
{
UITextField *txt = (UITextField*)[cell viewWithTag:0];
UITextField *txt1 = (UITextField*)[cell viewWithTag:1];
UITextField *txt2 = (UITextField*)[cell viewWithTag:2];
if(txt.isFirstResponder)
{
[txt resignFirstResponder];
[txt2 becomeFirstResponder];
}
else if(txt1.isFirstResponder)
{
[txt1 resignFirstResponder];
[txt becomeFirstResponder];
}
else if(txt2.isFirstResponder)
{
[txt2 resignFirstResponder];
[txt1 becomeFirstResponder];
}
}
Now in Input Accessory next button action do this:
-(IBAction)nextBtn:(id)sender
{
UITextField *txt = (UITextField*)[cell viewWithTag:0];
UITextField *txt1 = (UITextField*)[cell viewWithTag:1];
UITextField *txt2 = (UITextField*)[cell viewWithTag:2];
if(txt.isFirstResponder)
{
[txt resignFirstResponder];
[txt1 becomeFirstResponder];
}
else if(txt1.isFirstResponder)
{
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
else if(txt2.isFirstResponder)
{
[txt2 resignFirstResponder];
[txt becomeFirstResponder];
}
}
Related
I Created five UITextField using for loop i want to clear the textfield values when i Click the Clear button but only clear the last textfield values.i want to clear all text fiel values how can i solve this any one Help me Thanks
In your clear method write like:
for (UIView *addedView in [self.view subViews])
{
if([addedView isKindOfClass:[UITextField class]])
{
UITextField *myText = (UITextField *)addedView ;
myText.text = #"";
}
}
Here each sub view is taken from the self.view and check if it is a UITextField or not. If it is a UITextField then we set #"" to it.
for (UIView *view in self.view.subviews)
if([view isKindOfClass:[UITextField class]])
[(UITextField*) view setText:#""];
You can do this by using following:
-(void)loopMethod
{
lastTextField.tag=100;
}
-(void)buttonClick
{
UITextField *textField=(UITextField*)[self.view viewWithTag:100];
textField.text=NULL;
}
i want to add textfield on tableview each and access the textfield's text by using their tag.
how can i do that see below image.
// To get the text from UITextField , add the bellow code to after you initialize the Textfield
[yourTextField addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
and this method
- (void) getTextFieldValue:(UITextField *)textField{
NSLog(#"GetTextvalue");
if (textField.tag == 0) {
NSLog(#"1--->%#",textField.text);
}else if(textField.tag == 1){
NSLog(#"2--->%#",textField.text);
}else if(textField.tag == 2){
NSLog(#"3--->%#",textField.text);
}
}
You have to customize your tableViewCell you can do that in your cellForRowAtIndexPath method.
While you create your cell in the method you can do following:
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 101;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
[textField release];
After that where every you want to access your text field you can do following:
UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];
Hope this is what you are looking for..
Update
As you can see I assigned tag 101 as static. This can be any number but make sure that you don't have any other view with the same tag in the same cell. If you have in another cell than its not an issue.
Now at anyplace if lets say in didSelectRowAtIndexPath you can do following:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *myTextField = (UITextFiled*)[cell viewWithTag:101];
This way you will get your cell first. Now your cell has only one view with 101 tag so you can get your text field regardless you have same tag for other textField because the cell is not same for all.
Cheers
UITextField *myTextField = (UITextField*)[cell viewWithTag:yourTag];
myTextField.tag=indexPath.row;
This way assign 10 textfields tags.
After in textfield delegate method you can retrieve that text from the different 10 tags.
In my table I use UITextField istead of UILabel (with enabled = 0).
Now pressign an add button, the last cell shall become editable. I tried the following:
UITextField *textField = [subviewsOfCell ObjectAtIndex:index];
[textField becomeFirstResponder];
but I doesn't get it editable. (I know that I get the correct cell, because textField.text = #"test"; displays correctly). What must I do?
Just try doing this:
UITextField *textField = [subviewsOfCell ObjectAtIndex:index];
textField.enabled = YES;
[textField becomeFirstResponder];
Let me know if that works for you.
I am making an coredata application in which I was instructed to make a tableview (filling form) with textfield in each row which I did successfully. Then I made a toolbar with previous and next button and add to textField's inputAccsoryView for navigating between the textfields of each cell. One textfield per row . I am also able to do the magic with previous and next button methods . Now the problem :
Suppose I have 3 sections . In first section 4 rows . In second section 6 rows and in the last section 1 row . Now when I start from the 1st row and presses next till the 11th row it is working properly but then I press next nothing happens as the first row is dequeued I get "nil" for it . The code I am using :
configuring the cell
if (cell == nil) {
NSLog(#"tag inside:%i",tag);
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:identifier] autorelease];
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 170, 25)];
theTextField.textAlignment = UITextAlignmentRight;
theTextField.delegate = self;
theTextField.tag = tag;
theTextField.inputAccessoryView = [self configureTheToolBar];
[theTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[cell.contentView addSubview:theTextField];
[theTextField release];
}
cell.textLabel.text = rowLabel;
UITextField *textField = (UITextField *)[cell.contentView viewWithTag:tag];
textField.text = rowValue
I am providing tags for textfield like 601 ,602 etc .
Then getting the currentTextField tag
- (void)textFieldDidBeginEditing:(UITextField *)textField{
currentTextField = textField.tag;
}
then the next method
if(currentTextField == 611){
currentTextField = 601;
} else{
currentTextField = currentTextField + 1;
}
NSLog(#"current text fiedld = %i",currentTextField);
NSLog(#"text ; %#",[self cellForTag:currentTextField].textLabel.text);
UITextField *firstResponderField = (UITextField *) [[self cellForTag:currentTextField].contentView viewWithTag:currentTextField];
[firstResponderField becomeFirstResponder];
and the cellForTag method :
-(UITableViewCell *)cellForTag:(NSInteger)tag{
UITableViewCell *cell;
switch (tag) {
case 601:{
NSUInteger onlyRow[] = {0, 0};
NSIndexPath *onlyRowPath = [NSIndexPath indexPathWithIndexes:onlyRow length:2];
//[self.tableView scrollToRowAtIndexPath:onlyRowPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
cell = [self.tableView cellForRowAtIndexPath:onlyRowPath];
break;
}
return cell
this code is working partially as once off screen cells get out of memory I can't reach textfield in that cell and next button stops working till the currentTextfield value reaches to any visiblerow . What could be the solution . my stack is over flowed. :)
Ok this thread does the trick but I don't know how . can anyone explain me how ?
Once a cell goes off screen it will no longer be accessible. Because of this you might want to grab the values of your text fields as soon as the user has finished editing them by wiring up the UIControlEventEditingDidEnd event:
[myTextField
addTarget:self
action:#selector(myTextChanged:)
forControlEvents:UIControlEventEditingDidEnd];
In your myTextChanged function you can store the values away, perhaps in a dictionary if you want to keep them keyed against your tags.
In my iPhone project I'm using a UITableview with UITableViewCells containing UITextfields. I have seen in many apps that it is possible to use a next button to jump to the next textfield in the next cell. What is the best way to accomplish this?
My idea is to get the indexPath of the cell with the textfield that is being editing and then get the next cell by cellForRowAtIndexPath. But how can I get the indexPath of the cell I'm currently editing?
Thanks!
Keep references to the UITextField instances in your table view.
Assign unique tag values to your UITextField instances.
In your last text field, you might set its Return key type, which changes the keyboard's Return key label from "Next" to "Done": [finalTextField setReturnKeyType:UIReturnKeyDone];
In the UITextField delegate method -textFieldShouldReturn:, walk through the responders:
- (BOOL) textFieldShouldReturn:(UITextField *)tf {
switch (tf.tag) {
case firstTextFieldTag:
[secondTextField becomeFirstResponder];
break;
case secondTextFieldTag:
[thirdTextField becomeFirstResponder];
break;
// etc.
default:
[tf resignFirstResponder];
break;
}
return YES;
}
Assuming the UITextField was added to the UITableViewCell like below
UITableViewCell *cell;
UITextField *textField;
...
textField.tag = kTAG_TEXTFIELD;
[cell.contentView addSubview:textField];
...
You can get the current index path via
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if([textField.superView.superView isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cell = (UITableViewCell *)textField.superView.superView;
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
}
...
The next row's UITextField would then be
NSIndexPath *indexPathNextRow = [NSIndexPath indexPathForRow:(indexPath.row+1) inSection:indexPath.section];
UITableViewCell *cellNextRow = (UITableViewCell *)[self.myTableView cellForRowAtIndexPath:indexPathNextRow];
UITextField *textFieldNextRow = (UITextField *)[cellNextRow.contentView viewWithTag:kTAG_TEXTFIELD];
Hope it helps!