Get UITextField with a tag

I have the following code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (indexPath.row == 0) cell.tag = 0; else { cell.tag = 1; } if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UILabel *startDtLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)]; if (indexPath.row == 0) startDtLbl.text = @"Username"; else { startDtLbl.text = @"Password"; } startDtLbl.backgroundColor = [UIColor clearColor]; [cell.contentView addSubview:startDtLbl]; UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)]; passwordTF.delegate = self; if (indexPath.row == 0) passwordTF.tag = 2; else { passwordTF.tag = 3; } [cell.contentView addSubview:passwordTF]; } return cell; } 

I want to get a UITextField, how can I do this? I tried the following and it failed:

 UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0]; UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1]; UITextField * username = (UITextField*)[username_cell.contentView viewWithTag:2]; UITextField * password = (UITextField*)[password_cell.contentView viewWithTag:3]; NSLog(@"Username is %@", [username text]); NSLog(@"Password is %@", [password text]); 
+4
source share
5 answers

You should stop using tags to get cells. You must use indexPath for this.

replace

 UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0]; UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1]; 

with

 NSIndexPath *indexPathUserName = [NSIndexPath indexPathForRow:0 inSection:0]; UITableViewCell * username_cell = [self.tableView cellForRowAtIndexPath:indexPathUserName]; NSIndexPath *indexPathPassword = [NSIndexPath indexPathForRow:1 inSection:0]; UITableViewCell * password_cell = [self.tableView cellForRowAtIndexPath:indexPathPassword]; 

and if you want to reference a specific view, you cannot use the 0 tag. Since all tags that do not have custom tags have a 0 tag. So if you use viewWithTag: 0, you will get the last added view. And usually this is not the kind you want.

+6
source

If your text fields are inside uiscrollview and dynamically increase

 int i=0; for (UIView *subview in ContentScroll.subviews) { UITextField *textField = (UITextField*)[ subview viewWithTag:i]; NSLog(@"%@",textField.text); NSString *fieldValue = textField.text; if ([fieldValue isEqual:[NSNull null]]) { NSLog(@"as"); }else{ NSLog(@"Field %d has value: %@", i, fieldValue); } i++; } 
+2
source

I answered your question here with a UITableView

+1
source

Only the username and password are displayed in the table, so I suggest storing two pointers in the text fields that are created. This eliminates the need for methods. The code for marking the cells could be excluded, and the code that placed the text fields could be updated to save the pointer in the view controller.

  UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)]; passwordTF.delegate = self; if (indexPath.row == 0) self.passwordTextField = passwordTF; else { self.usernameTextField = passwordTF; } [cell.contentView addSubview:passwordTF]; 
+1
source

I think the problem is with the first line of this code:

Instead of this:

 UITableViewCell * username_cell = (UITableViewCell*)[self.view viewWithTag:0]; UITableViewCell * password_cell = (UITableViewCell*)[self.view viewWithTag:1]; 

Use this:

 UITableViewCell * username_cell = (UITableViewCell*)[self.tableView viewWithTag:0]; UITableViewCell * password_cell = (UITableViewCell*)[self.tableView viewWithTag:1]; 

Or whatever is called your table view. You are trying to get UITableViewCells from self.view , but UITableViewCells are subviews of tableview.

Alternative

If this does not work, you can find UITablViewCells using

 NSArray *arrTableViewCells = [tableView subviews]; 

and then get the UITextFields from these cells.

He should work

-1
source

Source: https://habr.com/ru/post/1340582/


All Articles