jack
jack

Reputation: 155

How to get all values from all textfields

After googling this issue i managed to do this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSString *cellIdentifier = @"InsertMarksCell";
    SaveAllExamMarksForAllStudentsTableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    StudentPersonalInfo *newStudentName = feedItemsNow[indexPath.row];

    myCell.txtStudentMark.text = @"hello";
    myCell.txtStudentMark.delegate = self;
    myCell.txtStudentMark.tag = indexPath.row;

    return myCell;
}

this code i this sets the textfield " txtStudentMark " delegate and sets it's tag ...

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    NSString *text = [(UITextField *)[self.view viewWithTag:55] text];
    NSLog(@"%@",text);
}

i keep getting null value using the NSLog Function

i have a textField in a custom cell which the user will fill with data and i need to get all data from all textfields, am i going the right way ?

from what i understood, i need to set the tag for textField and set it as delegate, then i can call textfield by tag to get the text in it .

how can i make this work ?

Upvotes: 2

Views: 774

Answers (2)

jack
jack

Reputation: 155

@Doro thanks for the help.

since the problem is solved, this is the answer that worked well for me

to get the row number and data in the textfield by TAG

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    // here get the tag number which is the row number
    NSInteger *rowNumberWithout200 = (long)textField.tag;
    NSString  *myRow = [NSString stringWithFormat: @"%ld", rowNumberWithout200];
    NSLog(@"row %@",myRow);

    // get the text in the cell with the required tag...
    UITableViewCell* myCell = (UITableViewCell*)textField.superview;
    NSString *StudentMark = [(UITextField *)[myCell viewWithTag:textField.tag] text];
    NSLog(@"mark %@",StudentMark);
}

Upvotes: 0

Doro
Doro

Reputation: 2413

Why don't you use

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    NSString *text = [textField text];
    NSLog(@"%@",text);
} 

?

Your problem is that you are adding textfield into cell, but ask view for this tag.

EDIT:

to get your textfield from tableview

-(void) textFieldDidEndEditing: (UITextField * ) textField
{
    UITableViewCell *cell = [self.view.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:55 inSection:0]];

    NSString *text = [(UITextField *)[cell.contentView viewWithTag:55] text];
    NSLog(@"%@",text);
}

Upvotes: 2

Related Questions