ios 如何检查视图控制器是否添加到堆栈中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42826026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check viewcontroller is added in stack or not
提问by Gaurav Gupta
I have two view controllers. I have navigated from one view to another view by press the button to using below code.
我有两个视图控制器。我通过按下按钮使用下面的代码从一个视图导航到另一个视图。
*let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("NotificationController") as! NotificationController
self.navigationController!.pushViewController(secondViewController, animated: true)*
For the back, I am using bar button on bar button click for back using below code.
对于背面,我使用下面的代码在条形按钮上单击条形按钮返回。
self.navigationController?.popViewControllerAnimated(true)
So my problem is if I am going from one view to another view continuously then it added in a stack. I want to only show another view when it is already added to the stack to stop adding it.It only adds one time.
所以我的问题是,如果我连续从一个视图转到另一个视图,那么它会添加到堆栈中。我只想在它已经添加到堆栈时显示另一个视图以停止添加它。它只添加一次。
回答by PGDev
To check whether the navigation stackcontains a particular type of view controller
, you can use:
要检查导航堆栈是否包含特定类型的view controller
,您可以使用:
if let viewControllers = self.navigationController?.viewControllers
{
if viewControllers.contains(where: {
return if var viewControllers = self.navigationController?.viewControllers
{
for controller in viewControllers
{
if controller is UIViewController
{
viewControllers.removeElement(controller)
self.navigationController?.viewControllers = viewControllers
}
}
}
is YourViewController
})
{
//Write your code here
}
}
To remove a particular controller from navigation stack, you need to make changes to the navigation stack.
要从导航堆栈中删除特定控制器,您需要对导航堆栈进行更改。
Example:
例子:
if let viewControllers = self.navigationController?.viewControllers {
for vc in viewControllers {
if vc.isKind(of: YourViewController.classForCoder()) {
print("It is in stack")
//Your Process
}
}
}
回答by vp2698
For swift 4 you can use
对于 swift 4,您可以使用
/// Given 'nc' is a valid UINavigationController instance,
/// removes all instances of MyViewController from the stack
nc.viewControllers = nc.viewControllers.filter { !(extension UINavigationController
{
/// Given the kind of a (UIViewController subclass),
/// removes any matching instances from self's
/// viewControllers array.
func removeAnyViewControllers(ofKind kind: AnyClass)
{
self.viewControllers = self.viewControllers.filter { !guard let nc = self.navigationController else { return }
let exists = nc.containsViewController(ofKind: MyViewController.self)
nc.removeAnyViewControllers(ofKind: MyViewController.self)
.isKind(of: kind)}
}
/// Given the kind of a (UIViewController subclass),
/// returns true if self's viewControllers array contains at
/// least one matching instance.
func containsViewController(ofKind kind: AnyClass) -> Bool
{
return self.viewControllers.contains(where: { if let viewControllers = navigationController?.viewControllers {
for viewController in viewControllers {
// some process
if viewController.isKindOfClass(ViewControllerClassName) {
println("yes it is")
}
}
}
.isKind(of: kind) })
}
}
is MyViewController) }
回答by Womble
Elaborating on PGDev's answer, for Swift 4.1
详细阐述 PGDev 的答案,适用于 Swift 4.1
How to remove a specific UIViewController
subclass from the UINavigationController
stack:
如何UIViewController
从UINavigationController
堆栈中删除特定的子类:
extension UINavigationController {
public func hasViewController(ofKind kind: AnyClass) -> UIViewController? {
return self.viewControllers.first(where: {self.navigationController.hasViewController(ofKind: #ViewControllerName#.self)
.isKind(of: kind)})
}
}
Putting it an an extension:
把它作为一个扩展:
NSArray * controllers = [self.navigationController viewControllers];
for (int i = 0; i < [controllers count]; i++){
UIViewController * controllerTest = [controllers objectAtIndex:i];
if([controllerTest isKindOfClass:[YourController class]]){
NSLog(@"Class is available");
}
}
Usage:
用法:
if let viewControllers = self.navigationController?.viewControllers {
for viewController in viewControllers {
// some process
if viewController.isKindOfClass(YourController) {
print("Class is available")
}
}
}
BTW, if anyone knows how to constrain 'kind' to subclasses of UIViewController, please yell out.
顺便说一句,如果有人知道如何将“种类”限制为 UIViewController 的子类,请大喊大叫。
回答by Sivajee Battina
Here is the code to check it.
这是检查它的代码。
if viewController.isKindOfClass(YourController){
}
回答by GameLoading
if let viewControllers = self.navigationController?.viewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(YourController) {
print("Your controller exist")
}
}
}
USE
用
self.navigationController?.popViewControllerAnimated(true)
回答by Abhishek Sharma
You can check with below code
您可以通过以下方式查看 code
Objective - C
目标-C
if self.navigationController?.accessibilityActivate() != nil {
self.navigationController?.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: nil)
}
Swift 3.0
斯威夫特 3.0
guard let controllersInStack = navigationController?.viewControllers else { return }
if let yourViewController = controllersInStack.first(where: { ##代码## is YourViewController }) {
// Do what you want with yourViewController
}
回答by Anil Kukadeja
Here we go.
开始了。
This line will give you a array of UIViewControllers
这一行会给你一个 UIViewControllers 数组
self.navigationController?.viewControllers
self.navigationController?.viewControllers
Now what you have to do is check your viewControllerObject does exist or not?
现在你要做的是检查你的 viewControllerObject 是否存在?
By writing this line
通过写这一行
##代码##and here is a complete code.
这是一个完整的代码。
##代码##When you write below line while going back to your 'ViewControllerA' it will remove a ViewControllerB from navigation stack.
当您在返回到“ViewControllerA”时在下面的行中写入时,它将从导航堆栈中删除一个 ViewControllerB。
##代码##It is just similar pop operation which we are doing with stack and navigationcontroller is a stack.
这与我们对堆栈进行的弹出操作类似,而导航控制器是一个堆栈。
Let me know if you have any confusions.
如果您有任何困惑,请告诉我。
回答by Nishad Arora
Swift 5(Working tested). Better way to check whether the view controller is stacked in navigation or not is by simply asking for the view controller's accessibility.See example below.I use this snippet to go back to previous view controller.
Swift 5(工作测试)。检查视图控制器是否在导航中堆叠的更好方法是简单地询问视图控制器的可访问性。请参见下面的示例。我使用此代码段返回上一个视图控制器。
##代码##回答by Faipdeoiad
Here's another solotion:
这是另一种解决方案:
##代码##