作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有某种方法可以在 SwiftUI 中实现等效于 popToViewController(vc)
?例如,如果我有以下流程:
View1 -> View2 -> View3 -> View4
考虑到我们无法控制导航堆栈,我如何才能从 View4 直接弹出到 View2?
最佳答案
Hello **@Milen Valchev**, I'm new to SwiftUI. As I'm doing R&D on some functionaities with SwiftUI. I'm facing issue with PopToSpecific View. After R&D I got something. You can use this for PopToSpecific View.
**1. First you've to create a extension for UINavigationController. check this below mentioned code**
import UIKit
extension UINavigationController {
func popToViewController(ofClass: AnyClass, animated: Bool = true) {
if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
popToViewController(vc, animated: animated)
}
}
func popViewControllers(viewsToPop: Int, animated: Bool = true) {
if viewControllers.count > viewsToPop {
let vc = viewControllers[viewControllers.count - viewsToPop - 1]
popToViewController(vc, animated: animated)
}
}
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
}
}
}
}
}
**2. After creating UINavigationController extension you've to get UINavigationController instance by using this code.**
let window = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.map { $0 as? UIWindowScene }
.compactMap { $0 }
.first?.windows
.filter { $0.isKeyWindow }
.first
let navigation = window?.rootViewController?.children.first as? UINavigationController
**Note:- Once you get the UINavigationController instance you can easily PopToSpecific View.**
You can use this code.
navigation.popViewControllers(viewsToPop: 2)
希望对你有帮助。
关于ios - SwiftUI - 如何在 SwiftUI 中弹出特定 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58813181/
我是一名优秀的程序员,十分优秀!