前幾天PM 提出一個需求,需求是A頁面點擊 Button A 會跳轉到 B 頁面, 而 B頁面有一個 Button B 點擊後會重新推出一個新的 A頁面。 所以依照這需求邏輯,當用戶不停的反覆 butoon A -> button B ,ViewController 會無限疊上去。 雖然最後了解這問題是故意設計的,不用處理,但是我還是稍微測試一下。
####測試
方案一、
navigationController?.popViewController(animated: false).
navigationController?.pushViewController(vc, animated: true)
在 push 之前做好邏輯判斷後,先退出之前的畫面再push。
- 成效:解決一半。
- 缺點:pop 雖然沒有動畫,但是還是看得到消失時產生的閃動。
方案二、
navigationController?.pushViewController(vc, animated: true)
removeSurplusViewController()
推出新畫面後,在篩選出多餘的ViewController ,從navigationController.viewControllers 中移除。
- 成效:完美解決。
- 缺點:稍微複雜點,頁面越多邏輯會越複雜。