You have several options you can either use NSNotificationCenter
or use the delegate pattern.
NSNotificationCenter is easy to use but also it is tricky.
To use notification center you need to add observers to your view controller classes.When you dismiss your modal view controller you notify your view 2 that view 3 is dismissed now, view2 can dismiss itself.....
So basically when you notify the center, whatever in notified it runs a method etc....
Lets say your at view 3, you want to dismiss your views.
in 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];
}
in view 2 . h
// For name of notification
extern NSString * const NOTIF_LoggingOut_Settings;
in view 2. m before @implementation
after#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];
}
add another observer to your first view
in your view1.h
extern NSString * const NOTIF_FirstView;
in view 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
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…