目标C语言 在点击单元格后编辑细节控制器中的文本域



在用户输入名称后编辑单元格中的名称时遇到了一点麻烦。

基本上他们点击一个按钮'add name',这把他们带到一个细节控制器,他们输入他们的姓和名,然后点击'done'。这将委托回主控制器并更新单元格以显示该名称。
我想要的是他们能够输入许多名称,然后点击一个单元格之后把他们带回他们已经输入的数据(这是存储在NSMutableArray称为'entry')

-(void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
     self.detailView.firstNameField.text = [[self.entry objectAtIndex:indexPath.row]firstName]];  
     NSLog(@"%@",[[self.entry objectAtIndex:indexPath.row]firstName]);
     NSLog(@"%@", self.detailView.firstNameField.text);
}

第一个NSLog显示名称很好,但第二个返回为"null",文本字段为空白,准备添加新名称…
非常感谢所有的帮助

我认为你这样做是错误的方式,它的工作方式,你不直接设置值到UITextfield,相反,你传递它一个NSString,这将是你的模型的属性。

这就是MVC的全部意义,数据不应该直接与UI相互作用。

在你的MasterViewController中,你需要导入DetailViewController标题:

#import "DetailViewController.h"

执行didSelectedRow中的segue:

-(void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
     [self performSegueWithIdentifier:@"detailSegue" sender:sender];
}

实现segue委托并设置它的firstName属性(NSSrting)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 // Make sure your segue name in storyboard is the same as this line
 if([segue.identifier isEqualToString:@"detailSegue"]){
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    DetailViewController*detailView = (DetailViewController*)segue.destinationViewController;
    detailVC.firstName =[[self.entry objectAtIndex:indexPath.row]firstName]]; 
    NSLog(@"%@",[[self.entry objectAtIndex:indexPath.row]firstName]);
  }
}

然后在viewDidLoad of DetailView中,你可以将firstName字符串分配给UItextfield文本属性:

_firstNameField.text = self.firstName;

最新更新