在关闭ipad中的ModalView后调用MasterView中的函数
问题描述:
我正在使用iPad的Master-Detail模板。我有一个视图控制器,我想模态显示,所以我用这个代码在关闭ipad中的ModalView后调用MasterView中的函数
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.m,我叫这行代码
[self dismissModalViewControllerAnimated:YES];
此代码也能正常工作和视图控制器被驳回,但驳回后,我想调用一个函数在我马西德威。怎么做?
根据与Moxy的讨论添加的代码。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.testViewController testfunction:testImage];
答
正如amit3117指出的那样,您应该使用委托。 协议应该至少用一种方法来定义,该方法可以与委托人沟通,以模态方式呈现的视图控制器完成其工作。
@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];
你必须使用NSNotification或创建马西德威的代表和来自调用它这里。 – Amit 2013-05-07 07:08:38
如果我不创建它,它将不会被调用,但我想知道,为什么它没有调用MasterView的VieWillAppear? – Ranjit 2013-05-07 07:10:39
你可以使用委托模式 – tkanzakic 2013-05-07 07:10:57