如何检测父视图控制器中模态视图控制器的解雇?

时间:2012-04-30 16:23:15

标签: iphone objective-c ios xcode modalviewcontroller

  

可能重复:
  Call Function in Underlying ViewController as Modal View Controller is Dismissed

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

-(void)viewWillAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidLoad
{

NSLog(@"Test");

}

为什么在取消模态视图控制器时,这些都不能在我的父视图控制器中工作?我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:114)

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

1。块

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

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。团

Delegation是Apple推荐的模式:

  

取消提供的视图控制器

     

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

<强> 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中的其他地方(被告知被解雇)

- (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:方法的协议可以在整个应用中重复使用)


3。通知

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

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

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

希望这有帮助!

答案 1 :(得分:10)

模态视图应该告诉其父级将其解雇,然后父母会知道,因为它负责解雇。

如果您创建一个新项目并选择Utility Application模板,则可以看到此示例。