ios 如何在 Swift 中弹出特定的视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30003814/
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 can I pop specific View Controller in Swift
提问by Mahesh Cheliya
I used the Objective-C
code below to pop a specific ViewController
.
我使用Objective-C
下面的代码弹出特定的ViewController
.
for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[AnOldViewController class]]) {
//Do not forget to import AnOldViewController.h
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
How can I do that in Swift?
我怎样才能在 Swift 中做到这一点?
回答by Mohit
Try following code:
尝试以下代码:
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
回答by Museer Ahamad Ansari
For Swift 3
对于 Swift 3
let viewControllers: [UIViewController] = self.navigationController!.viewControllers
for aViewController in viewControllers {
if aViewController is YourViewController {
self.navigationController!.popToViewController(aViewController, animated: true)
}
}
回答by Ashu
Swift 4.0
斯威夫特 4.0
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: DashboardVC.self) {
_ = self.navigationController!.popToViewController(controller, animated: true)
break
}
}
This is working Perfect.
这是完美的工作。
回答by PabloR
I prefer a generic way to do it.
我更喜欢通用的方法来做到这一点。
I have this extension for the UINavigationController :
我有 UINavigationController 的这个扩展:
extension UINavigationController {
func backToViewController(vc: Any) {
// iterate to find the type of vc
for element in viewControllers as Array {
if "\(element.dynamicType).Type" == "\(vc.dynamicType)" {
self.popToViewController(element, animated: true)
break
}
}
}
}
Let's say I have a FOHomeVC class (who is a UIViewController) instantiated in the navigation stack.
假设我在导航堆栈中实例化了一个 FOHomeVC 类(它是一个 UIViewController)。
So I would do this in my code:
所以我会在我的代码中这样做:
self.navigationController?.backToViewController(FOHomeVC.self)
回答by budidino
Swift 5
斯威夫特 5
To pop to the latest instance of a specific class, for example SomeViewController
:
要弹出到特定类的最新实例,例如SomeViewController
:
navigationController?.popToViewController(ofClass: SomeViewController.self)
But you need to add ths UINavigationController
extension:
但是您需要添加 thsUINavigationController
扩展名:
extension UINavigationController {
func popToViewController(ofClass: AnyClass, animated: Bool = true) {
if let vc = viewControllers.last(where: { extension UINavigationController {
func containsViewController(ofKind kind: AnyClass) -> Bool {
return self.viewControllers.contains(where: { for vc in self.navigationController!.viewControllers {
if let myViewCont = vc as? VCName
{
self.navigationController?.popToViewController(myViewCont, animated: true)
}
}
.isKind(of: kind) })
}
func popPushToVC(ofKind kind: AnyClass, pushController: UIViewController) {
if containsViewController(ofKind: kind) {
for controller in self.viewControllers {
if controller.isKind(of: kind) {
popToViewController(controller, animated: true)
break
}
}
} else {
pushViewController(pushController, animated: true)
}
}
}
.isKind(of: ofClass) }) {
popToViewController(vc, animated: animated)
}
}
}
回答by Parth Adroja
I have added an extension to UINavigationController
which helps you to find if that controller exist in navigation stack. If yes then it will be popped to that controller or else you pass new controller to push with pushController
param.
我添加了一个扩展来UINavigationController
帮助您查找导航堆栈中是否存在该控制器。如果是,那么它将弹出到该控制器,否则您将传递新控制器以使用pushController
参数推送。
let controllers : Array = self.navigationController!.viewControllers
self.navigationController!.popToViewController(controllers[1], animated: true)
回答by Chinju James
Find your view controller from navigation stack and pop to that view controller if it exists
从导航堆栈中找到您的视图控制器并弹出到该视图控制器(如果存在)
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: HomeViewController.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
回答by Mohsen Sedaghat Fard
swift5
迅捷5
@IBAction func popToConversationsVC(_ sender: UIButton) {
if (self.navigationController != nil) {
for vc in self.navigationController!.viewControllers {
if vc is ConversationsVC {
self.navigationController?.popToViewController(vc, animated: false)
}
}
}
}
回答by Pranit
Swift 4
斯威夫特 4
##代码##回答by Vijayvir Sing Pantlia
In latest swift
在最新的 swift
##代码##