[英]How to call method on presenting view controller from modal view controller
[英]modal view controller not calling presenting view controller's dismissModalViewControllerAnimated: method
在我的模态视图控制器中,我有一个按钮处理方法,其中包括
[self dismissModalViewControllerAnimated: YES];
在呈现的视图控制器中,我重写dismissModalViewControllerAnimated:,如下所示:
-(void) dismissModalViewControllerAnimated: (BOOL)animated
{
NSLog(@"dismiss");
[super dismissModalViewControllerAnimated: animated];
}
触摸按钮时,将调用按钮处理方法,但似乎未调用dismissModalViewControllerAnimated:覆盖:NSLog(@“ dismiss”); 语句不会被调用,并且方法内的断点不会被命中。
我试过了
[[self presentingViewController] dismissModalViewControllerAnimated: YES];
但这也不起作用。 但是,模态视图控制器确实被解雇了。
任何想法可能出了什么问题吗?
来自Programming iOS 6,作者Matt Neuburg :
在iPad上,当显示的视图控制器的modalPresentationStyle为UIModalPresentationCurrentContext时,必须确定哪个视图控制器应作为显示的视图控制器的presentingViewController。 这将确定将由呈现的视图控制器的视图替换哪个视图。 该决策涉及另一个UIViewController属性definePresentationContext(一个BOOL)。 从将presentViewController:animated:completion:发送到的视图控制器开始,我们沿着父级视图控制器的链,寻找其definePresentationContext属性为YES的父视图控制器。 如果我们找到一个,那就是那个。 它将是presentingViewController,其视图将由所呈现的视图控制器的视图替换。 如果找不到一个,那么事情就好像显示的视图控制器的modalPresentationStyle是UIModalPresentationFullScreen。
TL; DR
1.在所需的presentingViewController
definesPresentationContext
设置为true
2.组modalPresentationStyle
到UIModalPresentationCurrentContext
期望presentedViewController
这通常通过将您的呈现视图控制器声明为模式视图控制器的委托来处理。 然后,模式VC在当前的VC中调用了委托方法,以消除其创建的模式转换。
例:
模态VC.h:
@protocol ModalViewControllerDelegate
-(void)dismissMyModalViewController;
@end
模态VC.m:
// When you want to dismiss the Modal VC
[delegate dismissMyModalViewController];
呈现VC.h:
// Make sure to #import ModalVC.h
@property (nonatomic, retain) id <ModalViewControllerDelegate> delegate;
呈现VC.m:
-(void)dismissMyModalViewController {
[self dismissModalViewControllerAnimated:YES];
}
呈现模态视图控制器的代码包含在UIViewController中,而UIViewController又包含在UINavigationController中。 当我打电话
[[self presentingViewController] dismissModalViewControllerAnimated: YES];
要么
[self dismissModalViewControllerAnimated: YES];
解雇消息已发送到UINavigationController对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.