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

obj*_*ive 56 iphone xcode objective-c modalviewcontroller ios

可能重复:
底层ViewController中的调用函数作为模态视图控制器被解除

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

-(void)viewWillAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidLoad
{

NSLog(@"Test");

}
Run Code Online (Sandbox Code Playgroud)

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

Kir*_*sar 122

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

最简单的方法是使用回调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];
Run Code Online (Sandbox Code Playgroud)

SecondViewController.h中

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

SecondViewController.m中

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

    if (self.didDismiss) 
        self.didDismiss(@"some extra data");
}
Run Code Online (Sandbox Code Playgroud)

2.代表团

Delegation 是Apple推荐的模式:

解雇呈现的视图控制器

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

MainViewController

MainViewController.h中

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

MainViewController.m中的某个地方(呈现)

SecondViewController* svc = [[SecondViewController alloc] init];
svc.delegate = self;
[self presentViewController:svc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

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

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

SecondViewController

SecondViewController.h中

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

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

SecondViewController.m中的某个地方

[self.delegate didDismissViewController:self];
[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

(注意:带有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");
}
Run Code Online (Sandbox Code Playgroud)

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];
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 看起来"NSNotificationCenter"似乎有些过分,只有一个对象需要知道viewController何时被解除.我会说,由于过度复杂,它增加了额外的维护开销. (22认同)
  • Paul.s是对的,请查看Apple的文档:http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html (3认同)
  • 对于像这样的简单回调,您应该只添加一个调用父视图控制器的委托.通知意味着转到多个接收器,如登录状态已更改. (2认同)

Pau*_*l.s 10

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

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

  • 另外你一定不要看我提供给Apple的模板代码,因为他们使用委托而不仅仅是`self.parentViewController dismiss ...` (3认同)
  • 我做了`[self.parentViewController dismissModalViewControllerAnimated:YES];`但是` - (void)viewWillAppear:(BOOL)animated`仍然没有检测到它 (2认同)