0

Any way to know the navigation source?

For example, the navigation stack has A/B/C three view controllers. If C is popped, when B is displayed, any way to know the navigation is from C to B ?

Thanks a lot in advance.

san
  • 3,350
  • 1
  • 28
  • 40
Sanbrother
  • 601
  • 5
  • 12

3 Answers3

1

in another simple method

first declare the UINavigationController in Appdelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[nav setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
} 

after that in your first view controller.m import the second view controller header file

#import "B.h"

in your button action

- (IBAction)butvie:(id)sender {
B*tab=[[Balloc]init];
[self.navigationController pushViewController:tab
                                     animated:YES];
}

in C viewcontroller comes to back of B

- (IBAction)butvie:(id)sender {

[self.navigationController popViewController
                                     animated:YES];
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

You can track this manually. You can keep a global variable in your AppDelegate class and set that variable whenever you pop a particular viewController.

UPDATE after comments: In this case, you can use NSUserDefaults or you can post an NSNotification object from the poppedViewController. Though I am not sure, how much efficient these options are for you to use.

san
  • 3,350
  • 1
  • 28
  • 40
  • Yeah, that is just the way I am now using. While global or flag variables are not recommended by our team. – Sanbrother Dec 29 '13 at 04:28
  • In this case, you can use NSUserDefaults or you can post an NSNotification object from the poppedViewController. – san Dec 29 '13 at 04:32
0

You can tell whether a controller appeared because it was added to the stack, or because another controller was popped off the stack using isMovingToParentViewController. If you have this code in B, it will tell you which happened:

-(void)viewDidAppear:(BOOL)animated {
    if ([self isMovingToParentViewController]) {
        NSLog(@"Coming from A");
    }else{
        NSLog(@"Coming from C");
    }
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218