How to access UITextField in UITableView through tag in several functions
I have several UITextField in UITableView. I have assigned different tags to those fields but the 开发者_如何学JAVAproblem that i am facing is accessing those UITextField from several other functions.
How to access them? Here is my code:-
UITextField *textField = (UITextField *)[self.view viewWithTag:0];
Can anyone help?
You shouldn't be using 0 as the view tag. 0 will be the value for any view that hasn't had an explicit tag value set, so this won't pull out the view you are expecting.
I'd suggest you start your own tags from 100, although that is just an arbitrary round number.
But 0 definitely won't work as expected!
I have same feature in one of my apps and I used below code to accomplish that and I never had this kind of problem.
First of all you need to store all your textField value temporary in Array. Make array like this.
arrTemp=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],nil];
Then Give all textField tag = indexPath.row;
After that You need to replace textField value in below two methods.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}
At Last You need to set that value in cellForRowAtIndexPath datasource Method. So that whenever user scroll tableview it set previous value from temp array. Like this.
cell.txtEntry.text = [arrTemp objectAtIndex:indexPath.row];
精彩评论