retrieve text from textfield - iphone

i have
if(indexPath.row==0)
{
cell.lbl.text=#"abc";
cell.txtField.placeholder=#"abc"
cell.txtField.tag=104;
delegate.copyNameString = [NSString stringWithFormat:#"%#", [cell.txtField text]];
} else if(indexPath.row==1)
{
cell.lbl.text=#"home";
cell.txtField.placeholder=#"xyz";
cell.txtField.tag=105;
}
I am trying to retrieve the text which is in textfield, at row 0 and row 1.
i am trying to retrieve text on the textfield on the basis of tag.
How can i retrieve text?
Thanks

You can retrive your textfield like this and when you got your required textfield ,then get text.
UITableViewCell *myCell = (UITableViewCell *)[*yourTableViewName* cellForRowAtIndexPath:*passindexPath*]];
UITextField *mytextFiled = (UITextField *)[myCell viewWithTag:104];

the direct answer to your question is to use viewWithTag. In your example, [cell viewWithTag:105] will give you the text field. That said, your example has other issues.
First, if you can access the cell and the cell has a txtField property than you don't need the tag. Just use cell.txtField when you have the cell. Tags are typically used when you don't have a property holding a reference to the view you need. If you can't access the cell, the tag won't help you.
Second, you shouldn't add subviews directly to the cell. Instead you should add them to the cells contentView. The docs explain how to create a custom UITableViewCell fairly well.

NSString *textValue=((UITextField *)[self.view viewWithTag:104]).text;
You do like this,
UITextField *urtextFiled = (UITextField *)[self.view viewWithTag:104];
NSString *textValue=[urtextFiled text];

In which method you are trying to retrieve the text? I think you have to set delegate for your textfield and retrieve the text from this delegate method -(BOOL) textFieldShouldReturn:(UITextField *)textField

Related

How to find out programmatically added textfields in a scrollview

Inside my scrollView i am adding uitextfields programmatically by clicking an "ADD" Button. the number of textfields depending on how much i press in add button. it can be 'N' number of textfields.
After this fields editing how can i get values inside the all textfields into an array.
if once i edited textfield there is a possibility to delete entry inside that. So what is the way to save values in the all text fields while clicking in Save button.
i am attaching my screenshot with this.
Thanks in Advance
use fast enumeration.
NSMutableArray *arr = [NSMutablearray....];
for (UIView *subV in self.view.subviews){
if([subV isKindOfClass:[UITextField class]]){
//store it in a NSDictionary, so later can still know which
//textField your text belongs,
NSDictionary *tempDic = [NSDictionary dictionaryWithObjectAndKey:subV.txt
,subV.tag,/*or subVw.placeholder*/,nil];
[arr addObject:tempDic];
}
}
You can get all the textfields by iterating through the subview array of the scrollview
for (UIView *subView in scrollview) {
//check if the subView is a textfield by for example matching a certain tag
//save text of textfield
}
When you are creating textField then assign tagValue to every textField and then access textField using tag value....
UITextField *txtField = (UITextField *) [scrollView viewWithTag:tag];

UITableCellView viewWithTag

My problem:
I use IOS 5 and prototype cells.
In 'cellForRowAtIndexPath' function I set up my cell like that:
cell = [aTableView dequeueReusableCellWithIdentifier:#"datiInvioCell"];
UITextField *testo = (UITextField*)[cell viewWithTag:1];
testo.placeholder = iv.descrizione;
testo.text = iv.valore;
Now, I must store the result of the cell editing into an array. I used to do it like that:
-(void) textFieldDidEndEditing:(UITextField *)textField {
InputInvio *iv = [self.cellDettagli objectAtIndex:textField.tag];
iv.valore = textField.text;
}
but I can't use textField.tag anymore! Any idea? Thanks.
here you use static tag for your every textField so its get only one value which last.....
so here when you use bellow line just replace may be after that its work..
UITextField *testo = (UITextField*)[cell viewWithTag:indexPath.row];
here every textfield has its own tag...
hope , this help you..
:)

How to determine which section header in UITableView is being edited

I have a UITextField in a custom section header. There are multiple sections using this style of header, and therefore multiple UITextFields.
I have implemented the UITextFieldDelegate. When I edit one of these UITextFields, it calls the delegate method textFieldDidEndEditing. How do I determine which section header this UITextField was in? I need to save the value to core data in the appropriate NSManagedObject for that section.
Many thanks in advance
EDIT: Several people have suggested using a tag of the section number when creating the cell, which would work perfectly. However, I have already assigned the UITextField a tag to distinguish it as a 'header' textfield as opposed to a cell textfield or a 'footer' textfield. There are a whole lotta textfields on this table!!
Further EDIT: Using in indexPath has been suggested. This would be my preferred solution if I can get it to work. Does anyone know if headers and footers have indexPaths?
You could use tags to identify UITextField instances. Since you're already setting tags in UITextField instances, set the tags on the section views itself:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView = ... // your section view instance
// assign the section index as the tag
sectionView.tag = section;
return sectionView;
}
In the textfield delegate, get the section index from the sender's parent:
- (void) textFieldDidEndEditing:(UITextField *)textField;
{
NSInteger theSectionIndex = textField.superview.tag;
// your custom logic here
}
You would probably want to look into UITableView method indexPathForCell:.
You can get the cell through view hierarchy from your UITextField, since it's in your Cell's contentView.
Regards,
sven.
It is very simple Mr.Ben Thompson. You have named different between each one to UITextFields am i right?. Just find the specific UITextField by used it's name.
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == textfieldOne)
{
//Do whatever you want...
}
else if (textField == textfieldTwo)
{
//Do whatever you want...
}
else if (textField == textfieldThree)
{
//Do whatever you want...
}
}
I hope it will help you a little bit. Thanks.
i believe you add the field as a custom view to the header in viewForHeader method of tableview.
I suggest keeping a tag of the field using the section like this.
textfield.tag == section;
then in the delegate message you can have a switch method to compare tags..and do your own code there

objective-c: How to access textfield in first row of second section of Tableview?

I have a Tableview I want to change the value of textField on the first row of second section?
I assign it tag value which lets say is 2.
Now how to access that TextField with tag.
Give a tag to that text field and by using that tag you can change the value.
UITextField *tempField = (UITextField *) [self.view viewWithTag: tag];
tempField.text = #"Your data";
If you wnat to access your textField inside some table view methods so you can do this:-
UITextField *tView=(UITextField *)[cell.contentView viewWithTag:2];
If you have to access your textField outside some tableVIew method than you have to do :-
lastSelectedPath=indexPath;do where you have added your text field.You will need its indexPath.Make it a global variable.
UITableViewCell *prevoiusCell=(UITableViewCell *)[*yourtabelviewname* cellForRowAtIndexPath:lastSelectedPath];
UITextField *tView=(UITextField *)[prevoiusCell.contentView viewWithTag:2];

Retrieve UITextField Text for a specific UITextField Tag

Hey
Just wondering how do you..
Iv created some code that automatically creates a certain amount of UITextField input by the user.
Each UITextfield has a set tag automatically created.
The user then inputs his value into each of the UITextFields.
I want to retrieve the input text from the UITextFields which correspond to the tag.
I thought I nearly had it with:
NSString * tmpV = (NSString*)[choiceTextField.text viewWithTag:result];
where result is a increment, choiceTextField is the UITextField. With this I get a problem not defining instance, which I can't do as the UITextFields are generated in code not on the xib.
So to sum up, basically want the retrieve the text from a UITextField with specific tag.
Thanks
Dan
UITextField *yourTextField = (UITextField *)[self.view viewWithTag:result];
NSString *getText = myTextField.text;
Hope this helps
Your textfields must be a subview of a parent view. You need to write the following in your view controller of the parent view:
NSString *text = ((UITextField*)[self.view viewWithTag:result]).text;
Just go with Hetal Vora's solution: looks much cleaner!
-viewWithTag can be called on any view and searches all its subviews for one that matches the tag. Thus, you will find your UITextField by calling it on any view that is above it in the hierarchy. For example, you could call it on the view controller's view that contains your text field, as follows:
NSString *tmpV = [[myViewController.view viewWithTag:result] text];
If you are already in the view controller's class you could use:
NSString *tmpV = [[self.view viewWithTag:result] text];
On second thought, the following may be more correct & will avoid any compiler errors:
UITextField *myTextField = (UITextField *)[self.view viewWithTag:result];
NSString *tmpV = myTextField.text;