modal view controller not calling presenting view controller's dismissModalViewControllerAnimated: method

烈酒焚心 提交于 2019-11-29 16:48:19

This is normally handled by declaring your presenting view controller as a delegate for your modal view controller. The modal VC then called a delegate method in the presenting VC to dismiss the modal transition it created.

Example:

Modal VC.h:

@protocol ModalViewControllerDelegate
-(void)dismissMyModalViewController;
@end

Modal VC.m:

// When you want to dismiss the Modal VC
[delegate dismissMyModalViewController]; 

Presenting VC.h:

// Make sure to #import ModalVC.h
@property (nonatomic, retain) id <ModalViewControllerDelegate> delegate;

Presenting VC.m:

-(void)dismissMyModalViewController {
    [self dismissModalViewControllerAnimated:YES];
}

from Programming iOS 6, by Matt Neuburg:

On the iPad, when the presented view controller’s modalPresentationStyle is UIModalPresentationCurrentContext, a decision has to be made as to what view controller should be the presented view controller’s presentingViewController. This will determine what view will be replaced by the presented view controller’s view. This decision involves another UIViewController property, definesPresentationContext (a BOOL). Starting with the view controller to which presentViewController:animated:completion: was sent, we walk up the chain of parent view controllers, looking for one whose definesPresentationContext property is YES. If we find one, that’s the one; it will be the presentingViewController, and its view will be replaced by the presented view controller’s view. If we don’t find one, things work as if the presented view controller’s modalPresentationStyle had been UIModalPresentationFullScreen.

TL;DR
1. set definesPresentationContext to true on the desired presentingViewController
2. set modalPresentationStyle to UIModalPresentationCurrentContext on the desired presentedViewController

The code that presented the modal view controller was contained in a UIViewController, which was in turn contained in a UINavigationController. When I called

[[self presentingViewController] dismissModalViewControllerAnimated: YES];

or

[self dismissModalViewControllerAnimated: YES];

the dismissal message was being sent to the UINavigationController object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!