![](/img/trans.png)
[英]iOS 7 - Take the value of a textField inside a custom tableViewCell from another function
[英]ios get text value from textfield in tableViewCell
在ViewController中,我創建了一個名稱為UITextField *txtMyName;
的textField UITextField *txtMyName;
之后,我將我的textField添加到tableViewCell [cell addSubview:txtMyName];
我可以在textField中編輯文本,但是當我從textField中獲取文本時_strFullName = txtMyName.text;
但是,它得到的是空值。
我該如何解決這個問題!
*****編輯****
if (indexPath.row == 0) {
[self setFrameTextField:cell];
[self CustomTextField:cell :txtMyName];
if ([[dict_infoFriend objectForKey:@"Fullname"] isEqualToString:@" "]) {
txtMyName.placeholder = @"Name";
}
else{
txtMyName.text = [dict_infoFriend objectForKey:@"Fullname"];
}
[cell addSubview:txtMyName];
}
else if (indexPath.row == 1){
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
button.frame = CGRectMake(cell.frame.size.width - 10, 0, 130, 40);
}
else{
button.frame = CGRectMake(cell.frame.size.width / 2 - 65, 0, 130, 40);
}
[self CustomBTN:button];
[button addTarget:self action:@selector(ClickDone) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
}
您尚未為文本字段分配內存,這就是為什么您能夠編輯但無法檢索它的原因。 只需寫txtMyName = [[UITextfield alloc]init];
您可以將框架分配給txtMyName = [[UITextfield alloc]initWithFrame:CGRectMake(x,y,width,height)];
等txtMyName = [[UITextfield alloc]initWithFrame:CGRectMake(x,y,width,height)];
將內存分配給文本字段后,您將從文本字段中檢索文本
由於您已將文本字段添加到表格單元格中,因此必須首先獲取該單元格,然后使用文本字段的標記才能獲取文本。
這樣嘗試
假設您有文本字段的標簽,例如txtMyName.tag = 100
-(void) ClickDone {
NSIndexPath *indexPath = [[NSIndexPath alloc] indexPathForRow:0 inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];
UITextField *txtField = (UITextField *)[cell viewWithTag:100];
_strFullName = txtField.text;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.