popToViewController 引发“断言失败”留言,请问如何解决?

发布于 2024-08-24 10:51:47 字数 922 浏览 4 评论 0原文

在 CoreData 中,我有一些实体的数据图,每个对象都填充在视图控制器中,在定义的屏幕上,我想弹出一些(> 1)对象以返回到定义屏幕。

我尝试使用以下代码行将视图控制器从导航堆栈中弹出:

ObjectA *objectA = objectD.objectC.objectA;
NSLog(@"objectA name: %@", objectA.name);
MyViewController    *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil withObjectA:objectA];
[self.navigationController popToViewController:controller animated:YES];
[controller release];

objectA、objectB、objectC、objectD 都是我的数据图中的对象(通过逆关系,我可以通过 objectC 从 objectD 查询到 objectA )

出现以下错误消息:

Assertion failure in -[UINavigationController popToViewController:transition:], /SourceCache/UIKit_Sim/UIKit-984.38/UINavigationController.m:1807

这里有两个问题:

  • 如果两个对象不同,为什么它们具有相同的名称?逆关系取不回我用来初始化MyViewController的objectA?
  • 你通常如何做popToViewController?如何实现保存/加载导航控制器的当前状态,以便当应用程序退出时,我可以重新加载导航控制器?最佳实践是什么?

In CoreData, I have the data graph with some entities, and each object is populated in a view controller, at a defined screen, I want to pop out some (>1) objects to return to a define screen.

I tried to pop the view controllers out of the navigation stack with these lines of code:

ObjectA *objectA = objectD.objectC.objectA;
NSLog(@"objectA name: %@", objectA.name);
MyViewController    *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil withObjectA:objectA];
[self.navigationController popToViewController:controller animated:YES];
[controller release];

The objectA, objectB, objectC, objectD are all objects from my data graph(with the inverse relationship, I can query back the objectA from the objectD through objectC)

The following error message is raised:

Assertion failure in -[UINavigationController popToViewController:transition:], /SourceCache/UIKit_Sim/UIKit-984.38/UINavigationController.m:1807

There are two questions here:

  • If the two objects are different, how comes they have the same name? the inverse relationship can not get back the objectA in which I used to initialize MyViewController?
  • How do you normally do popToViewController? How can I implement save/load the current state of my navigation controller so that when the application quits, I can reload the navigation controller? What are the best practices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-08-31 10:51:47

这与核心数据无关。当您应该引用现有的 MyViewController 实例时,您正在创建一个新的视图控制器。新创建的控制器显然不在导航控制器的堆栈中,因此当您尝试切换到它时会出现异常。

使用 self.navigationController.viewControllers 数组来引用导航控制器堆栈中已有的控制器。

This has nothing to do with Core Data. You are creating a new view controller when you should just reference your existing MyViewController instance. The newly created controller is obviously not on the navigation controller's stack and so you get an exception when you try to switch to it.

Use the self.navigationController.viewControllers array to reference the controller that's already on the nav controller's stack.

放飞的风筝 2024-08-31 10:51:47

popToViewController:只能带您返回到 UINavigationController 堆栈中已有的 ViewController。您正在分配一个全新的控制器,然后尝试弹出它。

模型对象的核心数据对象图在这里并不直接相关;所有 popToViewController: 处理的是 ViewController 对象的堆栈。

要弹出到特定的 ViewController,您需要对该 ViewController 对象的引用。

popToViewController: can only take you back to an existing ViewController that's already on the UINavigationController's stack. You're allocating a completely new controller and then trying to pop to it.

The Core Data object graph of your model objects isn't directly relevant here; all popToViewController: is dealing with is the stack of ViewController objects.

To pop to a specific ViewController, you need a reference to that ViewController object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文