Reputation: 3682
I have a code of generating UITextField dynamically like this:
UITextField *myTextfield;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 640.0f, 0.0f)];
//for (int i=0; i<4; i++) {
myTextfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 60*i+20, 280, 45)];
myTextfield.text = [NSString stringWithFormat:@"Value: %d", section];
myTextfield.enabled = NO;
myTextField.tag = section;
[headerView addSubview:myTextField];
}
I also generated a edit button beside each of those textfields.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"cell1";
cell = (ButtonCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ButtonCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[ButtonCell class]])
cell = (ButtonCell *)oneObject;
}
cell.msg_btn.tag = indexPath.section;
cell.changecolor_btn.tag = indexPath.section;
cell.picklist_btn.tag = indexPath.section;
cell.adddelsubmenu_btn.tag = indexPath.section;
cell.done_btn.tag = indexPath.section;
[cell.msg_btn addTarget:self action:@selector(editEachTextField:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Now I want to enable and access each of the textfields when I click one of those buttons . I'm using tag to access those textfields like this:
- (void) editEachTextField:(UIButton*)sender
{
myTextfield = (UITextField *)[self.view viewWithTag:myTextfield.tag];
myTextfield.enabled = YES;
[myTextfield becomeFirstResponder];
NSLog(@"Tag value of Textfield: %ld",(long)myTextfield.tag);
NSString *text = [(UITextField *)[self.view viewWithTag:myTextfield.tag] text];
NSLog(@"Value: %@", text);
}
But whenever I click one of those button it returns tag value=4 and the 4th textfield is enabled for editing. What's the problem?
Upvotes: 2
Views: 1173
Reputation: 77
UITextField *txtfield = (UITextField *)sender;
NSLog(@"%ld",(long)txtfield.tag);
Upvotes: 0
Reputation: 3682
Finally Solved it by myself with the help of @Midhun MP and @Cyrille.
UITextField *myTextfield;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 640.0f, 0.0f)];
//for (int i=0; i<4; i++) {
myTextfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 60*i+20, 280, 45)];
myTextfield.text = [NSString stringWithFormat:@"Value: %d", section];
myTextfield.enabled = NO;
myTextField.tag = 400+section;
[headerView addSubview:myTextField];
}
and the button action selector:
- (void) editEachTextField:(UIButton*)sender
{
myTextfield = (UITextField *)[self.view viewWithTag:400+sender.tag];
myTextfield.enabled = YES;
[myTextfield becomeFirstResponder];
NSString *text = [(UITextField *)[self.view viewWithTag:400+sender.tag] text];
NSLog(@"Value: %@", text);
}
Upvotes: 0
Reputation: 107121
You are taking myTextfield.tag
value for enabling the text field instead of clicked button's tag.
The myTextfield
points to the last allocated UITextField
(Which has tag 4), that's why you are getting tag value as 4 and the last textfield is getting enabled.
Issue is with this code:
myTextfield = (UITextField *)[self.view viewWithTag:myTextfield.tag];
Change that to:
myTextfield = (UITextField *)[self.view viewWithTag:sender.tag];
Note:
Don't use same tag for button and textfield if both are placed in the same view. If done so, you can't ensure which object you'll be getting when the viewWithTag
is used.
Upvotes: 3