-segueForUnwindingToViewController:fromViewController:标识符:不被调用
我创build了一个自定义的segue,它提供了一个与Apple自己的模式视图控制器非常相似的容器内的视图控制器(我已经将它实现为一个UIViewController子类)。
我现在试图创build一个自定义展开segue,但是我不能得到方法-segueForUnwindingToViewController: fromViewController: identifier:
被调用。
我也已经实现了-viewControllerForUnwindSegueAction: fromViewController: withSender:
在我的容器上,所以我可以指向正确的视图控制器(呈现此模式的那个),但然后应该问我的自定义unwind segue的方法不会被调用任何地方。
现在,我唯一能解决这个问题的方法就是用-returned:
方法去做。
有没有人可以成功做到这一点与自定义放松segue?
编辑:多一点的代码和上下文
我的展开视图控制器在故事板中configuration,而不是编程。
我有我的控制器中unwind segues相关的代码段:
PresenterViewController.m
我在这里使用自定义的方法来解除我的自定义模式( -dismissModalViewControllerWithCompletionBlock:
。
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier { return [[MyModalUnwindSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController]; } -(IBAction)returned:(UIStoryboardSegue *)segue { if ([segue.identifier isEqualToString:@"InfoUnwindSegue"]) { [self dismissModalViewControllerWithCompletionBlock:^{}]; } }
MyModalViewController.m
在这里我只使用-viewControllerForUnwindSegueAction: fromViewController: withSender:
指向视图控制器,我应该放松。
- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender { return self.myPresentingViewController; }
我期待的行为是调用MyModalViewController指向应该处理展开的视图控制器,然后这个视图控制器有他的-segueForUnwindingToViewController: fromViewController: identifier:
方法之前调用 – -returned:
被调用。
现在-segueForUnwindingToViewController: fromViewController: identifier:
永远不会被调用。
我还必须说,我已经尝试了不同的configuration。 每当我把我的方法返回放松segue它永远不会被调用。 我读过,我可以inheritance导航控制器,然后它被调用,但我不知道它是如何适合我的解决scheme。
编辑2:其他信息
我已经检查了MyModalViewController有他的-segueForUnwindingToViewController: fromViewController: identifier:
方法调用时,我想要消除它提出的常-segueForUnwindingToViewController: fromViewController: identifier:
态视图控制器。 这可能是因为他是层次结构中最顶层的UIViewController。
在检查了这个我已经子类UINavigationController并使用这个子类,而不是包含我的PresenterViewController。 我很惊讶地注意到他的-segueForUnwindingToViewController: fromViewController: identifier:
方法也被调用。
我相信只有作为容器的视图控制器才有这种方法。 这对我来说没什么意义,因为他们并不是唯一提供其他视图控制器的人,他们的孩子也在这样做。
在这个子类中创build逻辑来select使用哪个segue类是不好的,因为这个类不知道他们的孩子做了什么。
苹果论坛暂时closures,所以现在无法得到他们的支持。 如果任何人有关于如何工作的更多信息,请帮助! 我想缺乏这方面的文件是很不稳定的一个很好的指标。
要添加到@Jeremy的答案,我从一个模式的UIViewController展开到UINavigationController中包含的UIViewController正常工作(即我如何期待它)使用以下内我的UINavigationController子类。
// Pass to the top-most UIViewController on the stack. - (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *) toViewController fromViewController:(UIViewController *) fromViewController identifier:(NSString *)identifier { UIViewController *controller = self.topViewController; return [controller segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; }
然后在UINavigationController里实际的ViewController中像往常一样实现segueForUnwindingToViewController。
这个方法应该在父控制器上声明。 因此,如果您使用带有自定义segue的导航控制器,请使用子类UINavigationController并在其上定义此方法。 如果您想在UINavigationController的子视图之一上定义它,您可以在UINavigationController上覆盖canViewerler:withViewController:withSender来让它search一个处理程序。
如果您正在使用embedded视图(容器视图),那么在父视图控制器上定义它。
请参阅WWDC 2012 Session 407的最后10分钟 – 在应用程序中采用情节串联板来了解其原因!
如果你使用的是UINavigationController,并且你的segue调用了pushViewController
那么为了使用自定义的unwind segue,你将需要- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
UINavigationController并覆盖- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
。
说我有一个自定义的放松segue,叫做。 我的UINavigationController子类的实现可能看起来像这样:
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier { UIStoryboardSegue* theSegue; if ([identifier isEqualToString:@"CloseDoor"]) { theSegue = [CloseBookSegue segueWithIdentifier:identifier source:fromViewController destination:toViewController performHandler:^(void){}]; } else { theSegue = [super segueForUnwindingToViewController:toViewController fromViewController:fromViewController identifier:identifier]; } return theSegue;
}
将您的UINavigationController子类设置为故事板中的导航控制器类。 如果你已经正确设置了Exit事件,那么你应该很好用“以”作为标识符。 此外,请务必在您的unwind segue中调用“popViewControllerAnimated”,而不要忽略与UINavigationControllers的push / pop机制保持一致。
iOS开发库有关iOS开发库和这种方法的讨论。 – segueForUnwindingToViewController:fromViewController:identifier:确保您的MyModalViewController是容器angular色,而不是容器的子控制器。 如果有[anotherVC addChildViewController:myModalViewController];
,你应该把segueForUnwindingToViewController方法放在某种“AnotherVC.m”文件中。
讨论如果您实现也使用segue展开的自定义容器视图控制器,则必须重写此方法。 您的方法实现应该实例化并返回一个自定义的segue对象,该对象执行展开视图控制器所需的任何animation和其他步骤。