您有几个选项可以使用NSNotificationCenter
或使用委托模式。
NSNotificationCenter易于使用但也很棘手。
要使用通知中心,您需要将观察者添加到您的视图控制器类。当您关闭模式视图控制器时,您会通知您的视图 2 视图 3 现在已关闭,view2 可以自行关闭.....
所以基本上当你通知中心时,无论通知什么,它都会运行一个方法等......
假设您在视图 3 中,您想驳回您的观点。
在 view3 .m
-(IBAction)yourMethodHere
{
//dissmiss view
[self.navigationController dismissModalViewControllerAnimated:YES];
// or [self dismissModalViewControllerAnimated:YES]; whateever works for you
//send notification to parent controller and start chain reaction of poping views
[[NSNotificationCenter defaultCenter] postNotificationName:@"goToView2" object:nil];
}
在视图 2 中。H
// For name of notification
extern NSString * const NOTIF_LoggingOut_Settings;
在视图 2.m 之前@implementation
之后#imports
NSString * const NOTIF_LoggingOut_Settings = @"goToView2";
@implementation
-(void)viewDidAppear:(BOOL)animated{
// Register observer to be called when logging out
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loggingOutSettings:)
name:NOTIF_LoggingOut_Settings object:nil];
}
/*---------------------------------------------------------------------------
* Notifications of 2 view
*--------------------------------------------------------------------------*/
- (void)loggingOutSettings:(NSNotification *)notif
{
NSLog(@"Received Notification - Settings Pop Over popped");
[self.navigationController popViewControllerAnimated:NO];// change this if you do not have navigation controller
//call another notification to go to view 1
[[NSNotificationCenter defaultCenter] postNotificationName:@"goToFirstView" object:nil];
}
在 view1.h 的第一个视图中添加另一个观察者
extern NSString * const NOTIF_FirstView;
在视图中 1.m before @implementation
after#imports
NSString * const NOTIF_FirstView = @"goToFirstView";
@implementation
-(void)viewDidAppear:(BOOL)animated{
// Register observer to be called when logging out
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doYourThing:)
name:NOTIF_FirstView object:nil];
}
/*---------------------------------------------------------------------------
* Notifications of 1 view
*--------------------------------------------------------------------------*/
- (void)ldoYourThing:(NSNotification *)notif
{
// do your thing
}