gpt4 book ai didi

ios - 在 ipad 中关闭 ModalView 后调用 MasterView 中的函数

转载 作者:行者123 更新时间:2023-11-29 03:56:35 24 4
gpt4 key购买 nike

我正在使用 iPad 的主从模板。我有一个 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 指出的那样,您应该使用委托(delegate)。该协议(protocol)至少应该使用一种方法来定义,该方法可以向委托(delegate)传达以模态方式呈现的 View Controller 已完成其工作的信息。

@class ViewController;

@protocol MyViewControllerDelegate <NSObject>

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

@end

编辑:我忘记添加您应该为 ViewController 的委托(delegate)提供一个公共(public)属性

@interface ViewController : UIViewController

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

@end

您可以使用主视图 Controller 作为委托(delegate)。因此,在您的主视图 Controller 实现中,您还会有:

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

关于ios - 在 ipad 中关闭 ModalView 后调用 MasterView 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com