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];
}
希望这有帮助!