我发现很多方法可以使用Objective-C在UIViewControllers
中弹回2 UINavigationController
,但是当我尝试将其切换到Swift时,它似乎没有用。
回到UIViewController
的最佳方法是什么?
任何指导将不胜感激
感谢
答案 0 :(得分:103)
扩展我的评论,找到viewControllers数组中的第二个最后一个视图控制器,然后使用popToViewController来避免覆盖整个视图控制器堆栈。
示例(假设导航控制器有多个视图控制器):
func backTwo() {
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 3], animated: true)
}
<强>目标C 强>
NSArray *viewControllers = [self.navigationController viewControllers];
[self.navigationController popToViewController:viewControllers[viewControllers.count - 3] animated:YES];
答案 1 :(得分:15)
我写了一个UIViewController扩展(Swift 3+ ready)
您可以这样使用:
/// pop back n viewcontroller
func popBack(_ nb: Int) {
if let viewControllers: [UIViewController] = self.navigationController?.viewControllers {
guard viewControllers.count < nb else {
self.navigationController?.popToViewController(viewControllers[viewControllers.count - nb], animated: true)
return
}
}
}
用法:
self.popBack(3)
奖励解雇特定的视图控制器
/// pop back to specific viewcontroller
func popBack<T: UIViewController>(toControllerType: T.Type) {
if var viewControllers: [UIViewController] = self.navigationController?.viewControllers {
viewControllers = viewControllers.reversed()
for currentViewController in viewControllers {
if currentViewController .isKind(of: toControllerType) {
self.navigationController?.popToViewController(currentViewController, animated: true)
break
}
}
}
}
用法:
self.popBack(toControllerType: MyViewController.self)
答案 2 :(得分:11)
您可以在(swift 2.0)
中动态弹出多个viewcontrollerslet allViewController: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
for aviewcontroller : UIViewController in allViewController
{
if aviewcontroller .isKindOfClass(YourDestinationViewControllerName)
{
self.navigationController?.popToViewController(aviewcontroller, animated: true)
}
}
答案 3 :(得分:11)
user5320485回答
let viewControllers = self.navigationController!.viewControllers as [UIViewController];
for aViewController:UIViewController in viewControllers {
if aViewController.isKind(of: AdCreateViewController.self) {
_ = self.navigationController?.popToViewController(aViewController, animated: true)
}
}
答案 4 :(得分:1)
@Maximelc的答案的Swift 4 UINavigationController扩展版本:
extension UINavigationController {
func popBack(_ nb: Int) {
let viewControllers: [UIViewController] = self.viewControllers
guard viewControllers.count < nb else {
self.popToViewController(viewControllers[viewControllers.count - nb], animated: true)
return
}
}
/// pop back to specific viewcontroller
func popBack<T: UIViewController>(toControllerType: T.Type) {
var viewControllers: [UIViewController] = self.viewControllers
viewControllers = viewControllers.reversed()
for currentViewController in viewControllers {
if currentViewController .isKind(of: toControllerType) {
self.popToViewController(currentViewController, animated: true)
break
}
}
}
}
答案 5 :(得分:0)
为了方便起见,我做了一个助手:
import Foundation
import UIKit
class NavigationHelper {
static func back(number: Int, from viewControlller : UIViewController) {
let viewsBack = number + 1
let viewControllers: [UIViewController] = viewControlller.navigationController!.viewControllers as [UIViewController]
viewControlller.navigationController!.popToViewController(viewControllers[viewControllers.count - viewsBack], animated: true)
}
static func back(number: Int, from viewControlller: UITableViewController) {
let viewsBack = number + 1
let viewControllers: [UIViewController] = viewControlller.navigationController!.viewControllers as [UIViewController]
viewControlller.navigationController!.popToViewController(viewControllers[viewControllers.count - viewsBack], animated: true)
}
}
例如,在您的ViewController
中,您可以使用NavigationHelper.back(number: 1, from: self)
答案 6 :(得分:0)
这里是另一个稍微“防呆”的版本:
extension UINavigationController {
func popBack(_ count: Int) {
guard count > 0 else {
return assertionFailure("Count can not be a negative value.")
}
let index = viewControllers.count - count - 1
guard index > 0 else {
return assertionFailure("Not enough View Controllers on the navigation stack.")
}
popToViewController(viewControllers[index], animated: true)
}
}
答案 7 :(得分:0)
let allViewControllers : [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
self.navigationController?.popToViewController(allViewControllers[allViewControllers.count-3], animated: true)
答案 8 :(得分:0)
如果您想在特定的视图控制器上弹出,您可以通过在示例代码中将 DestinationViewController 替换为您的视图控制器来搜索该视图控制器。
func moveToDestinationViewController() {
let viewController = self.navigationController?.viewControllers.first { $0 is DestinationViewController }
guard let destinationVC = viewController else { return }
self.navigationController?.popToViewController(destinationVC, animated: true)
}