Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Last active January 28, 2021 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodycatliu/e138c10a3edc36d9fa8df727971fa542 to your computer and use it in GitHub Desktop.
Save woodycatliu/e138c10a3edc36d9fa8df727971fa542 to your computer and use it in GitHub Desktop.

跳轉頁面那件小事

前幾天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 中移除。

  • 成效:完美解決。
  • 缺點:稍微複雜點,頁面越多邏輯會越複雜。

短短幾行的小範例

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment