在closuresipad中的ModalView后,在MasterView中调用函数

我正在使用iPad的Master-Detail模板。 我有一个ViewController,我想模式显示,所以我用这个代码

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; m_ViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; m_ViewController.modalPresentationStyle = UIModalPresentationFormSheet; [appDelegate.splitViewController presentModalViewController:m_ViewController animated:YES]; 

这工作正常和ViewController加载模态,现在我试图解雇这个ViewController,所以在ViewController.m,我叫这行代码

 [self dismissModalViewControllerAnimated:YES]; 

此代码也工作正常,ViewController被解雇, 解雇后,我想调用我的MasterView函数。 怎么做?

根据与Moxy的讨论添加代码。

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [appDelegate.testViewController testfunction:testImage]; 

正如amit3117指出的,你应该使用一个委托。 该协议应至less定义一个方法,它将与委托人沟通,以模态方式呈现的视图控制器完成其工作。

 @class ViewController; @protocol MyViewControllerDelegate <NSObject> -(void)viewControllerDidFinish:(ViewController *)sender; @end 

编辑:我忘了添加,你应该为ViewController委托公共财产

 @interface ViewController : UIViewController @property (nonatomic, weak) id <MyViewControllerDelegate> delegate; @end 

你可以使用你的主视图控制器作为委托。 所以在你的主视图控制器实现中,你也可以:

 @interface MyMasterViewController () <MyViewControllerDelegate> @end @implementation MyMasterViewController -(void)showViewController { m_ViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; m_ViewController.modalPresentationStyle = UIModalPresentationFormSheet; m_ViewController.delegate = self; // –presentModalViewController:animated: is deprecated! [self.parentViewController presentViewController:m_ViewController animated:YES completion:nil]; } -(void)viewControllerDidFinish:(ViewController *)sender { // Add any code you want to execute before dismissing the modal view controller // –dismissModalViewController:animated: is deprecated! [self.parentViewController dismissViewControllerAnimated:YES completion:^{ // code you want to execute after dismissing the modal view controller }]; } @end 

m_ViewController完成它的工作时,它应该调用:

 [self.delegate viewControllerDidFinish:self];