如何在父视图控制器中检测到模态视图控制器的解除? [英] How can I detect the dismissal of a modal view controller in the parent view controller?

查看:83
本文介绍了如何在父视图控制器中检测到模态视图控制器的解除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

基础ViewController中的调用函数作为模态视图控制器被关闭

我几乎尝试了一切。这是我尝试过的:

I've tried almost everything. Here's what I've tried:

-(void)viewWillAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidLoad
{

NSLog(@"Test");

}

为什么这些都不能在我的父视图控制器中工作模态视图控制器被解雇了?我怎样才能使这个工作?

Why are none of these working in my parent view controller when the modal view controller is dismissed? How can I get this to work?

推荐答案

这个答案被重写/扩展以解释3个最重要的方法( @galambalazs

最简单的方法是使用回调 block 。如果您只有一个侦听器(父视图控制器)对解雇感兴趣,这是很好的。您甚至可以通过该事件传递一些数据。

The simplest approach is using a callback block. This is good if you only have one listener (the parent view controller) interested in the dismissal. You may even pass some data with the event.

MainViewController.m

SecondViewController* svc = [[SecondViewController alloc] init];
svc.didDismiss = ^(NSString *data) {
    // this method gets called in MainVC when your SecondVC is dismissed
    NSLog(@"Dismissed SecondViewController");
};
[self presentViewController:svc animated:YES completion:nil];

SecondViewController.h

@interface MainViewController : UIViewController
    @property (nonatomic, copy) void (^didDismiss)(NSString *data);
    // ... other properties
@end

SecondViewController.m

- (IBAction)close:(id)sender 
{
    [self dismissViewControllerAnimated:YES completion:nil];

    if (self.didDismiss) 
        self.didDismiss(@"some extra data");
}






2。代表团



委派 是Apple推荐的模式:


2. Delegation

Delegation is the recommended pattern by Apple:


解雇呈现的视图控制器

如果呈现的视图控制器必须将数据返回到呈现视图控制器,使用委托设计模式,方便转移。委派可以更轻松地在应用程序的不同部分重用视图控制器。通过委托,呈现的视图控制器存储对委托对象的引用,该委托对象实现来自正式的方法协议。在收集结果时,呈现的视图控制器会在其委托上调用这些方法。在典型的实现中,呈现视图控制器使自己成为其呈现的视图控制器的委托。

If the presented view controller must return data to the presenting view controller, use the delegation design pattern to facilitate the transfer. Delegation makes it easier to reuse view controllers in different parts of your app. With delegation, the presented view controller stores a reference to a delegate object that implements methods from a formal protocol. As it gathers results, the presented view controller calls those methods on its delegate. In a typical implementation, the presenting view controller makes itself the delegate of its presented view controller.

MainViewController

MainViewController.h

@interface MainViewController : UIViewController <SecondViewControllerDelegate>
    - (void)didDismissViewController:(UIViewController*)vc;
    // ... properties
@end

中的某处MainViewController.m (呈现)

SecondViewController* svc = [[SecondViewController alloc] init];
svc.delegate = self;
[self presentViewController:svc animated:YES completion:nil];

MainViewController.m 中的其他地方(被告知解雇)

Somewhere else in MainViewController.m (being told about the dismissal)

- (void)didDismissViewController:(UIViewController*)vc
{
    // this method gets called in MainVC when your SecondVC is dismissed
    NSLog(@"Dismissed SecondViewController");
}

SecondViewController

SecondViewController.h

@protocol SecondViewControllerDelegate <NSObject>
- (void)didDismissViewController:(UIViewController*)vc;
@end

@interface SecondViewController : UIViewController
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
// ... other properties
@end

某处> SecondViewController.m

[self.delegate didDismissViewController:self];
[self dismissViewControllerAnimated:YES completion:nil];

(注意:带有didDismissViewController:方法的协议可以在整个应用中重复使用)

另一种解决方案是发送 NSNotification 。这也是一种有效的方法,如果你只想在没有传递太多数据的情况下通知关于解雇的话,它可能比委托更容易。但它的主要用例是当您想要解雇事件的多个侦听器时(除了父视图控制器之外)。

Another solution is sending an NSNotification. This is a valid approach as well, it might be easier than delegation in case you only want to notify about the dismissal without passing much data. But it's main use case is when you want multiple listeners for the dismissal event (other than just the parent view controller).

但请确保完成后始终 NSNotificationCentre 中删除自己!否则,即使在取消分配后,通过调用通知也可能导致崩溃。 [编者注]

But make sure to always remove yourself from NSNotificationCentre after you are done! Otherwise you risk of crashing by being called for notifications even after you are deallocated. [editor's note]

MainViewController.m

- (IBAction)showSecondViewController:(id)sender 
{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self presentViewController:secondVC animated:YES completion:nil];

    // Set self to listen for the message "SecondViewControllerDismissed"
    // and run a method when this message is detected
    [[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(didDismissSecondViewController)
     name:@"SecondViewControllerDismissed"
     object:nil];
}

- (void)dealloc
{
    // simply unsubscribe from *all* notifications upon being deallocated
    [[NSNotificationCenter defaultCenter] removeObserver:self];
} 

- (void)didDismissSecondViewController 
{
    // this method gets called in MainVC when your SecondVC is dismissed
    NSLog(@"Dismissed SecondViewController");
}

SecondViewController.m

- (IBAction)close:(id)sender 
{
    [self dismissViewControllerAnimated:YES completion:nil];

    // This sends a message through the NSNotificationCenter 
    // to any listeners for "SecondViewControllerDismissed"
    [[NSNotificationCenter defaultCenter] 
     postNotificationName:@"SecondViewControllerDismissed" 
     object:nil userInfo:nil];
}

希望这会有所帮助!

这篇关于如何在父视图控制器中检测到模态视图控制器的解除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆