这个答案被改写/扩展以解释3种最重要的方法(
@galambalazs
)
1。阻碍
最简单的方法是使用回调
block
. 如果你只有
一个听众
(父视图控制器)对解雇感兴趣。您甚至可以在事件中传递一些数据。
在
主视图控制器.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];
在
第二视图控制器.h
@interface MainViewController : UIViewController
@property (nonatomic, copy) void (^didDismiss)(NSString *data);
// ... other properties
@end
在
第二视图控制器.m
- (IBAction)close:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
if (self.didDismiss)
self.didDismiss(@"some extra data");
}
2。代表团
Delegation
苹果推荐的模式是:
解除显示的视图控制器
如果呈现视图控制器必须将数据返回到呈现视图控制器,请使用
delegation
设计模式以便于传输。委派使在应用程序的不同部分重新使用视图控制器变得更容易。通过委托,呈现的视图控制器存储对委托对象的引用,该对象实现了来自
protocol
. 当它收集结果时,显示的视图控制器在其委托上调用这些方法。在一个典型的实现中,呈现视图控制器使自己成为其呈现视图控制器的委托。
主视图控制器
在
主视图控制器.h
@interface MainViewController : UIViewController <SecondViewControllerDelegate>
- (void)didDismissViewController:(UIViewController*)vc;
// ... properties
@end
在某个地方
主视图控制器.m
(呈现)
SecondViewController* svc = [[SecondViewController alloc] init];
svc.delegate = self;
[self presentViewController:svc animated:YES completion:nil];
在别的地方
主视图控制器.m
(被告知解雇)
- (void)didDismissViewController:(UIViewController*)vc
{
// this method gets called in MainVC when your SecondVC is dismissed
NSLog(@"Dismissed SecondViewController");
}
辅助视图控制器
在
第二视图控制器.h
@protocol SecondViewControllerDelegate <NSObject>
- (void)didDismissViewController:(UIViewController*)vc;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
// ... other properties
@end
在某个地方
第二视图控制器.m
[self.delegate myActionFromViewController:self];
[self dismissViewControllerAnimated:YES completion:nil];
(注意:使用didismissViewController:method的协议可以在整个应用程序中重用)
三。通知
另一个解决方案是发送
NSNotification
. 这也是一个有效的方法,如果您只想这样做的话,它可能比委托更容易。
通知
关于解雇而没有传递太多数据。但它的主要用途是在你想要的时候
多个侦听器
对于解雇事件(而不仅仅是父视图控制器)。
但一定要
总是
把你自己从
N通知中心
完成后!否则,即使在解除分配之后,仍有被调用通知的风险。
[编者按]
在
主视图控制器.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");
}
在
第二视图控制器.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];
}
希望这有帮助!