[英]How to find all the view controllers before my current viewcontroller in iOS?
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. 任何建议将不胜感激。
I had similar issue in one of my projects and I created a singleton class which maintains a stack of ViewControllers. 在我的一个项目中,我遇到了类似的问题,并且创建了一个单例类,该类维护着一组ViewController。 Each ViewController is responsible for pushing and popping itself from the stack.
每个ViewController负责将自身推入堆栈并从堆栈中弹出。 Thus, we queried stack whenever we wanted to have flow that user followed.
因此,只要我们希望用户遵循该流程,我们就会查询堆栈。
It worked pretty fine for us. 它对我们来说很好。
You can check your controller by comparing it, Put your desired class instead of "XYZViewController" 您可以通过比较来检查控制器,将所需的类而不是“ XYZViewController”
for (UIViewController * aController in self.navigationController.viewControllers)
{
if (aController isKindOfClass:[XYZViewController class])
{
//You can do your work here
}
}
You can use the navigation stack in this way to get to know which viewController are there before you reach certain View controller. 您可以通过这种方式使用导航堆栈,以在到达某些View控制器之前了解哪个viewController。
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. 但是,如果要显示标签,如果用户来自某个VC,并且从其他ViewController进入VC时不显示标签,则在需要显示标签的ViewController中使用布尔值,然后设置从上一个VC推送时,布尔值为YES,并检查该布尔值以显示标签。
In the current view controller's viewDidAppear()
, viewWillAppear()
or the viewDidLoad()
method use the following code. 在当前视图控制器的
viewDidAppear()
, viewWillAppear()
或viewDidLoad()
方法中,使用以下代码。 I have used it my viewDidLoad() method. 我已经使用它了我的viewDidLoad()方法。
for (int i=0;i<[self.navigationController.viewControllers count];i++)
{
NSLog(@"%d>>>>>>>>>%@",[self.navigationController.viewControllers objectAtIndex:i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.