Shishir.bobby
Shishir.bobby

Reputation: 10984

retrieve text from textfield

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

Upvotes: 1

Views: 310

Answers (4)

iDev
iDev

Reputation: 11

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

Upvotes: 0

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

NSString *textValue=((UITextField *)[self.view viewWithTag:104]).text;

You do like this,

 UITextField *urtextFiled = (UITextField *)[self.view viewWithTag:104];
NSString *textValue=[urtextFiled text];

Upvotes: 0

Gypsa
Gypsa

Reputation: 11314

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

Upvotes: 1

XJones
XJones

Reputation: 21967

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.

Upvotes: 0

Related Questions