场景:快速多次点击cell跳转到另一个页面,另一个页面被push多次。
原因:push后的页面有耗时操作或者刚好push到另一个页面时,另一个页面正好在reloadData卡住主线程。造成点击cell时卡住了。
解决方法:重写导航控制器的push方法
// BaseNavigationViewController.swift
import UIKit
class BaseNavigationViewController: UINavigationController,UINavigationControllerDelegate {
var pushing = false
override func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = nil
self.delegate = self
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
// MARK:UINavigationControllerDelegate
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
self.pushing = false
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if self.pushing{
return//被拦截
} else {// push 控制器
self.pushing = true
super.pushViewController(viewController, animated: true)
}
}
}