Calling a Function in MasterView after dismissing the ModalView in ipad

扶醉桌前 提交于 2019-12-11 20:41:51

问题


I am using Master-Detail template for ipad. I have a ViewController, which I want to show modally so I have used this code

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

This works fine and the ViewController is loaded modally, Now I tried to dismiss this ViewController, So inside ViewController.m, I called this line of code

[self dismissModalViewControllerAnimated:YES];

This code also works fine and the ViewController gets dismissed, But after dismissing I want to call a function in my MasterView. How to do that?

Code added according to the discussion with Moxy.

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

回答1:


As amit3117 pointed out, you should use a delegate. The protocol should be defined at least with a method that would communicate to the delegate that the view controller that was presented modally did finish its work.

@class ViewController;

@protocol MyViewControllerDelegate <NSObject>

-(void)viewControllerDidFinish:(ViewController *)sender;

@end

EDIT : I forgot to add that you should a public property for the delegate to ViewController

@interface ViewController : UIViewController

@property (nonatomic, weak) id <MyViewControllerDelegate> delegate;

@end

You could use your master view controller as the delegate. So in your master view controller implementation you would also have :

@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

When m_ViewController finishes its work, it should call :

[self.delegate viewControllerDidFinish:self];


来源:https://stackoverflow.com/questions/16413147/calling-a-function-in-masterview-after-dismissing-the-modalview-in-ipad

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