0

我正在为 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];
4

1 回答 1

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];
于 2013-05-07T07:32:50.043 回答