您有几个选项可以使用NSNotificationCenter 或delegate 模式。
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;
在@implementation 之前#imports#imports 之前的视图 2.m
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];
}
在您的第一个视图中添加另一个观察者
在你看来1.h
extern NSString * const NOTIF_FirstView;
在@implementation之前的视图1.m之后#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
}