在ipad中解除ModalView后,在MasterView中调用一个函数

时间:2013-05-07 07:05:45

标签: ios ipad uisplitviewcontroller master-detail

我正在使用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];

1 个答案:

答案 0 :(得分:1)

正如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];