0

There are different paths which a user can take to get to a particular view controller in my app.
I want to know the exact path taken by the User to get to the current view controller.

Any suggestion would be highly appreciated.

1
  • 1
    you can maintain a stack of ViewControllers in a singelton class. Commented Sep 29, 2016 at 11:35

4 Answers 4

2

I had similar issue in one of my projects and I created a singleton class which maintains a stack of ViewControllers. Each ViewController is responsible for pushing and popping itself from the stack. Thus, we queried stack whenever we wanted to have flow that user followed.

It worked pretty fine for us.

Sign up to request clarification or add additional context in comments.

Comments

2

You can check your controller by comparing it, Put your desired class instead of "XYZViewController"

for (UIViewController * aController in self.navigationController.viewControllers) { if (aController isKindOfClass:[XYZViewController class]) { //You can do your work here } } 

4 Comments

Well I was looking for a general approach which can be used in all the situations.
you can set a variable in AppDelegate and compare the desired one class through it
@MdIbrahimHassan what was wrong with this solution? you should accept this one.
@SaadChaudhry This would not work well if I have multiple view controllers of the same class type. So I guess a more general answer would be better like the first one itself or the third one from the top
1

You can use the navigation stack in this way to get to know which viewController are there before you reach certain View controller.

Here is the Code :

for (UIViewController *vc in self.navigationController.viewControllers) { NSLog(@"vc desc : %@", vc.description); } 

But if you want to show a label , if the user come from a certain VC and not to show the label when come to the VC from other ViewController, Then use a boolean in the ViewController in which you need to show the label, then set the boolean as YES, while pushing from previous VC and check that boolean to show the label.

Comments

0

In the current view controller's viewDidAppear(), viewWillAppear() or the viewDidLoad() method use the following code. I have used it my viewDidLoad() method.

 for (int i=0;i<[self.navigationController.viewControllers count];i++) { NSLog(@"%d>>>>>>>>>%@",[self.navigationController.viewControllers objectAtIndex:i]); } 

5 Comments

The only problem with this is : you cannot track it, it is simply just a logger for console.
I need just for checking the views the user has been through and show a label if the user has been through a viewcontroller in the hierarchy.
for that you can add a global or local bit which will check if the user has came through current viewcontroller
@SaadChaudhry Exactly I didn't of that. But I think my current approach would also work for the given situation.
The only issue is, you would have to write it in every view Controller. Might consider AOP here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.