我在尝试使用作为子级添加到另一个 View Controller 的 View Controller 中的自定义转场进行展开时遇到问题。
这是 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");
}
所有必要的连接也在 Storyboard中完成。
我的问题是-segueForUnwindingToViewController:
从未被调用过。
一旦-viewControllerForUnwindSegueAction:fromViewController:withSender:
返回后,我的程序崩溃并出现以下异常:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not find a view controller to execute unwinding for <FirstViewController: 0x8e8d560>'
据我了解,崩溃的原因是我希望我的容器 View Controller 能够处理展开转场操作,这是不可能的(因为容器 View Controller 仅询问其子级 处理展开转场。
正确吗? 我可以做什么来解决我的问题?
谢谢!
最佳答案
我认为你不能以这种方式使用展开转场(至少我找不到办法)。相反,您可以创建另一个从子 Controller 返回到父 Controller 的“正常”segue,并将其类型设置为自定义,并使用与从第一个 Controller 到第二个 Controller 相同的类。在第二个 Controller 中的prepareForSegue中,将isPresenting设置为NO,并将此代码放入您的自定义segue中,
- (void)perform {
if (_isPresenting) {
NSLog(@"Presenting");
ViewController *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;
} completion:^(BOOL finished){
[toVC didMoveToParentViewController:fromVC];
}];
}else{
NSLog(@"dismissing");
SecondVC *fromVC = self.sourceViewController;
[fromVC willMoveToParentViewController:nil];
[UIView animateWithDuration:1.0 animations:^{
fromVC.view.alpha = 0;
} completion:^(BOOL finished) {
[fromVC removeFromParentViewController];
}];
}
}
关于ios - 容器 View Controller 无法处理 Unwind Segue 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911404/