![](/img/trans.png)
[英]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.