简体   繁体   English

切换视图时,核心数据使属性为NULL

[英]Core Data Makes attribute NULL when switching views

I have four views. 我有四个观点。

View 1, shows a table view with an add button. 视图1,显示带有添加按钮的表格视图。 (Add button not visible, it is above screen shot bounds) (添加按钮不可见,它在屏幕快照的边界上方)

查看1

View 2 ( presented when add button is pressed) allows the user to input data about the table view cell they would like to add. 视图2(在按下添加按钮时显示)允许用户输入有关他们要添加的表视图单元的数据。 Once the save button is pressed, the new cell is added to the view. 按下save按钮后,新单元格将添加到视图中。

查看2

View 3 If the user would like to edit a cell, they just tap the cell. 查看3如果用户要编辑单元格,则只需点击该单元格。 This view Just allows the user to edit the cell that is already in the table. 此视图仅允许用户编辑表中已经存在的单元格。

查看3

But when i simply put view 4 in between view 1 (the table view) and 3 (the cell editor view), the attributes/labels show null where the user input is supposed to be shown. 但是,当我只是将视图4放在视图1(表视图)和视图3(单元格编辑器视图)之间时,属性/标签显示为null ,应该显示用户输入。 But when i remove view 4 from in between view 1 and 3, the app works perfect again. 但是,当我从视图1和视图3之间删除视图4时,该应用程序再次正常运行。 Any reasons? 有什么原因吗?

查看4

I am simply switching views via: 我只是通过以下方式切换视图:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

View3 * three = (View3 *) [storyboard instantiateViewControllerWithIdentifier:@"view3VC"];

View4 * four = (View4 *) [storyboard instantiateViewControllerWithIdentifier:@"view4VC"];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

three.managedObjectContext = self.context;

MyEntityClass *entity = [self.tableArray objectAtIndex:indexPath.row];

three.3entity = entity;

[self presentViewController:four animated:YES completion:nil];

}

It's a bit confusing but I think I would try to help you. 这有点令人困惑,但我想我会尽力帮助您。

In your code: 在您的代码中:

MyEntityClass *entity = [self.tableArray objectAtIndex:indexPath.row];
three.3entity = entity;

You said it works when you call view3, I guess it is because you've assign the entity to the view3. 您说它在调用view3时有效,我想这是因为您已将实体分配给view3。

But if you present view4 like this, you didn't assign anything to it between. 但是,如果您以这种方式显示view4,则您之间没有分配任何内容。

View4 * four = (View4 *) [storyboard instantiateViewControllerWithIdentifier:@"view4VC"];

AND

[self presentViewController:four animated:YES completion:nil];

Now view4 is a totally fresh and empty view controller. 现在,view4是一个全新的空视图控制器。 It's not strange that it shows null in your labels. 它在标签中显示null并不奇怪。

Try fix it in the way exactly as view3. 尝试以与view3完全相同的方式对其进行修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM