我在尝试使用自定义 segue 从作为子项添加到另一个视图控制器的视图控制器中放松时遇到问题。
这是 MyCustomSegue.m:
- (void)perform
{
if (_isPresenting)
{
//Present
FirstVC *fromVC = self.sourceViewController;
SecondVC *toVC = self.destinationViewController;
toVC.view.alpha = 0;
[fromVC addChildViewController:toVC];
[fromVC.view addSubview:toVC.view];
[UIView animateWithDuration:1.0 animations:^{
toVC.view.alpha = 1;
//fromVC.view.alpha = 0;
} completion:^(BOOL finished){
[toVC didMoveToParentViewController:fromVC];
}];
}
else
{
//Dismiss
}
}
这是我的 FirstVC.m:
- (void)prepareForSegue:(MyCustomSegue *)segue sender:(id)sender
{
segue.isPresenting = YES;
}
- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
{
return self;
}
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
return [[MyCustomSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
}
- (IBAction)unwindToFirstVC:(UIStoryboardSegue *)segue
{
NSLog(@"I am here");
}
所有必要的连接也都在情节提要中完成。
我的问题是-segueForUnwindingToViewController:
永远不会被调用。一旦-viewControllerForUnwindSegueAction:fromViewController:withSender:
返回,我的程序就会崩溃,并出现以下异常:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not find a view controller to execute unwinding for <FirstViewController: 0x8e8d560>'
据我了解,崩溃的原因是我希望我的容器视图控制器成为处理 unwind segue 动作的控制器,这是不可能的(因为容器视图控制器只要求其子级处理 unwind segue。
这是对的吗?我能做些什么来解决我的问题?
谢谢!