Iphone 如何检测父视图控制器中模态视图控制器的取消?

Iphone 如何检测父视图控制器中模态视图控制器的取消?,iphone,objective-c,ios,xcode,modalviewcontroller,Iphone,Objective C,Ios,Xcode,Modalviewcontroller,可能重复: 我几乎什么都试过了。以下是我尝试过的: -(void)viewWillAppear:(BOOL)animated { NSLog(@"Test"); } -(void)viewDidAppear:(BOOL)animated { NSLog(@"Test"); } -(void)viewDidLoad { NSLog(@"Test"); } 当模态视图控制器被解除时,为什么这些都不能在我的父视图控制器中工作?我如何才能让它工作?模态视图应该告诉它的父视图解除它,然

可能重复:

我几乎什么都试过了。以下是我尝试过的:

-(void)viewWillAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidLoad
{

NSLog(@"Test");

}

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

模态视图应该告诉它的父视图解除它,然后父视图就会知道,因为它负责解除

如果您创建了一个新项目并选择了
实用程序
模板,则可以看到一个例子。

此答案被改写/扩展,以解释3种最重要的方法()

1.阻碍 最简单的方法是使用回调。如果您只有一个侦听器(父视图控制器)感兴趣,这是很好的。您甚至可以在事件中传递一些数据

在MainViewController.m中

在SecondViewController.h中

在SecondViewController.m中


2.授权 是苹果公司推荐的模式:

取消显示的视图控制器

如果显示视图控制器必须将数据返回到显示视图控制器,请使用设计模式来促进传输。委派使在应用程序的不同部分重用视图控制器变得更容易。通过委托,呈现的视图控制器存储对委托对象的引用,委托对象实现了形式化视图中的方法。在收集结果时,显示的视图控制器在其委托上调用这些方法。在典型的实现中,呈现视图控制器使自己成为其呈现视图控制器的代理

MainViewController

在MainViewController.h中

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

SecondViewController

在SecondViewController.h中

(注意:使用didDismissViewController:method的协议可以在整个应用程序中重复使用)


3.通知 另一个解决方案是发送一个电子邮件。这也是一种有效的方法,如果您只想通知解雇而不传递太多数据,那么它可能比委托更容易。但它的主要使用情形是,当您希望解雇事件(而不仅仅是父视图控制器)有多个侦听器时

但是,请确保在完成后始终将自己从NSNotificationCenter中移除!否则,即使在您解除分配后,您也有被呼叫通知而崩溃的风险。[编者按]

在MainViewController.m中

在SecondViewController.m中


希望这有帮助

对不起,我误解了这个问题。我做了[self.parentViewController dismissModalViewControllerAnimated:是]但是<代码>-(无效)视图将出现:(BOOL)动画
仍然没有检测到它否,但您知道模式已被删除,因此,您在
视图中运行的任何代码都将出现:
您希望在视图出现和删除模式时运行的代码应该放在它自己的方法中,该方法从两个位置调用。此外,您一定没有看过我指给您的苹果提供的模板代码,由于他们使用委托而不仅仅是
self.parentViewController disclose…
这是指向支持您所说内容的苹果文档的链接:似乎“NSNotificationCenter”在这里有些过火,只有一个对象需要知道viewController何时被解除。我想说,由于过于复杂,它增加了额外的维护开销。Paul.s是对的,请查看苹果的文档:对于这样一个简单的回调,您应该只添加一个调用父视图控制器的委托。通知将发送给多个接收者,如登录状态已更改。如果使用此方法,请确保在调用didDismissSecondViewController之后删除self:[[NSNotificationCenter defaultCenter]removeObserver:self name:@“SecondViewControllerDismissed”对象:nil];我的代表没有接到电话!
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];
@interface MainViewController : UIViewController
    @property (nonatomic, copy) void (^didDismiss)(NSString *data);
    // ... other properties
@end
- (IBAction)close:(id)sender 
{
    [self dismissViewControllerAnimated:YES completion:nil];

    if (self.didDismiss) 
        self.didDismiss(@"some extra data");
}
@interface MainViewController : UIViewController <SecondViewControllerDelegate>
    - (void)didDismissViewController:(UIViewController*)vc;
    // ... properties
@end
SecondViewController* svc = [[SecondViewController alloc] init];
svc.delegate = self;
[self presentViewController:svc animated:YES completion:nil];
- (void)didDismissViewController:(UIViewController*)vc
{
    // this method gets called in MainVC when your SecondVC is dismissed
    NSLog(@"Dismissed SecondViewController");
}
@protocol SecondViewControllerDelegate <NSObject>
- (void)didDismissViewController:(UIViewController*)vc;
@end

@interface SecondViewController : UIViewController
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
// ... other properties
@end
[self.delegate didDismissViewController:self];
[self dismissViewControllerAnimated:YES completion:nil];
- (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");
}
- (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];
}