我正在尝试将我们的应用程序转换为故事板,并且在处理自定义容器控制器时遇到了我认为处理展开segue的错误.我们有一个视图控制器,它显示另一个并使用视图控制器包含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];
}];
}
Run Code Online (Sandbox Code Playgroud)
这完全有效,但是我不能让它执行解开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;
}
Run Code Online (Sandbox Code Playgroud)
我还可以确认canPerformUnwindSegueAction:fromViewController:正在调用withSender并做正确的事情,但要确保我重写它以返回YES
-(BOOL) canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender {
return YES;
} …Run Code Online (Sandbox Code Playgroud)