Possible Duplicate:
Call Function in Underlying ViewController as Modal View Controller is Dismissed

我几乎什么都试过了.以下是我try 过的:

-(void)viewWillAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidAppear:(BOOL)animated
{

NSLog(@"Test");

}

-(void)viewDidLoad
{

NSLog(@"Test");

}

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

推荐答案

This answer was rewritten/expanded to explain the 3 most important approaches (100)

1.街区

最简单的方法是使用回调block.如果只有one listener(父视图控制器)感兴趣,这是很好的.你甚至可以在活动中传递一些数据.

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是苹果公司推荐的模式:

Dismissing a Presented View Controller

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

MainViewController

MainViewController.h年后

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

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

百分之一百左右

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

(note: the protocol with didDismissViewController: method could be reused throughout your app)


3.通知

另一个解决方案是发送NSNotification.这也是一种有效的方法,它可能比授权更容易,以防你只想在不传递太多数据的情况下谈论解雇事宜.但它的主要使用情形是,当您希望multiple listeners用于解雇事件(而不仅仅是父视图控制器)时.

但一定要在完成后将自己从NSNotificationCentre中移除always!否则,即使在你被解除分配后,你也有被要求通知的风险.[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];
}

希望这有帮助!

Objective-c相关问答推荐

Objective C 布尔数组

如何检测 iPhone 5c 的 colored颜色 ?

禁用手势识别器

在 10.9 上以编程方式启用对辅助设备的访问

为什么当我使其处于非活动状态时,弱 IBOutlet NSLayoutConstraint 变为 nil?

静态和动态单元格的 UITableView 混合?

UITableView 无限滚动

使用自动布局以编程方式更改框架

仅替换 NSString 中子字符串的第一个实例

Objective-C:如何使用 BOOL 类型的参数调用 performSelector?

在 Xcode 中打破 EXC_BAD_ACCESS?

iOS 内存分配 - 一个应用程序可以使用多少内存?

iOS:使用设备修饰符加载 xib 文件?

如何将 NSNumber 转换和比较为 BOOL?

如何在 Cocoa 中获取 NSArray 的第一个 x 元素?

计算 UILabel 文本大小

无法加载从设备上运行的包中的 nib 引用的图像

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

在 iOS 6.0 的 Xcode 4.5 中我没有要求的日志(log)消息

UITableViewCell中的文本居中对齐问题