我正在尝试将我们的应用程序转换为情节提要,并且在处理自定义容器控制器时遇到了我认为处理展开 segues 的错误。我们有一个视图控制器,它显示另一个并使用视图控制器包含 api 来执行此操作,我在 IB 中连接 segue,然后为实现选择一个自定义类。perform 方法如下所示:
-(void) perform {
UIViewController *container = [self sourceViewController];
UIViewController *child = [self destinationViewController];
[container addChildViewController:child];
[container.view addSubview:child.view];
child.view.center = container.view.center;
[UIView transitionWithView:container.view
duration:0.35
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
child.view.alpha = 1;
} completion:^(BOOL finished) {
[child didMoveToParentViewController:container];
}];
}
这完美地工作,但是我不能让它执行 unwind segue 回到容器控制器。我覆盖 viewControllerForUnwindSegueAction: fromViewController: withSender: 并确保它返回正确的值:
-(UIViewController *) viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender {
id default = [super viewControllerForUnwindSegueAction:action fromViewController:fromViewController withSender:sender];
NSAssert1(default == self, @"Expected the default view controller to be self but was %@", default);
return default;
}
我还可以确认 canPerformUnwindSegueAction:fromViewController:withSender 被调用并做正确的事情,但要确保我覆盖它以返回 YES
-(BOOL) canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender {
return YES;
}
我希望发生的下一步是调用 segueForUnwindingToViewController:fromViewController:identifier:,但它永远不会。相反,应用程序因 NSInternalInconsistencyException 而崩溃。
2012-10-01 10:56:33.627 UnwindSegues[12770:c07] *** Assertion failure in -[UIStoryboardUnwindSegueTemplate _perform:], /SourceCache/UIKit_Sim/UIKit-2372/UIStoryboardUnwindSegueTemplate.m:78
2012-10-01 10:56:33.628 UnwindSegues[12770:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not find a view controller to execute unwinding for <USCustomContainerViewController: 0x75949a0>'
*** First throw call stack:
(0x1c8e012 0x10cbe7e 0x1c8de78 0xb61f35 0x581711 0x45ab54 0x10df705 0x16920 0x168b8 0xd7671 0xd7bcf 0xd6d38 0x4633f 0x46552 0x243aa 0x15cf8 0x1be9df9 0x1be9ad0 0x1c03bf5 0x1c03962 0x1c34bb6 0x1c33f44 0x1c33e1b 0x1be87e3 0x1be8668 0x1365c 0x1e7d 0x1da5)
libc++abi.dylib: terminate called throwing an exception
有没有人成功地使用了结合视图控制器包含 API 的 unwind segues?知道我错过了哪一步吗?我已经将一个演示项目上传到 github,它在我能想到的最简单的演示项目中显示了这个问题。