防止 ViewController 移回上一个
Preventing ViewController From Shifting Back to Previous
我有一个带有两个视图控制器 (UIViewController
) 的简单示例 iOS 应用程序,如下所示。
//HomeViewController
import UIKit
class HomeViewController: UIViewController {
// MARK: - IBAction
@IBAction func goSecondTapped(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "SecondNavigator") as? UINavigationController {
if let viewController = controller.topViewController as? SecondViewController {
self.navigationController?.pushViewController(viewController, animated: true)
}
}
}
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
}
//SecondViewController
import UIKit
class SecondViewController: UIViewController {
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
}
所以我只想在点击按钮(转到第二个)时切换到 SecondViewController。问题是,如果我只是用手指从左向右滑动第二个视图,我就可以在不点击左上角的“后退”按钮的情况下返回到 HomeViewController。有什么办法可以防止这种情况发生吗?我只想在点击返回时返回 HomeViewController。谢谢
您是否尝试过禁用 interactivePopGestureRecognizer
?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
我有一个带有两个视图控制器 (UIViewController
) 的简单示例 iOS 应用程序,如下所示。
//HomeViewController
import UIKit
class HomeViewController: UIViewController {
// MARK: - IBAction
@IBAction func goSecondTapped(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "SecondNavigator") as? UINavigationController {
if let viewController = controller.topViewController as? SecondViewController {
self.navigationController?.pushViewController(viewController, animated: true)
}
}
}
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
}
//SecondViewController
import UIKit
class SecondViewController: UIViewController {
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
}
}
所以我只想在点击按钮(转到第二个)时切换到 SecondViewController。问题是,如果我只是用手指从左向右滑动第二个视图,我就可以在不点击左上角的“后退”按钮的情况下返回到 HomeViewController。有什么办法可以防止这种情况发生吗?我只想在点击返回时返回 HomeViewController。谢谢
您是否尝试过禁用 interactivePopGestureRecognizer
?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}