Swift - Navigator Push Present
๐ ๋ชฉ์ฐจ
- ์๋ก
- push
- present
- back
๐ ์๋ก
์ค๋์ swift์์์ ํ๋ฉด์ ํ์ ๋ํด์ ์์๋ณด๊ฒ ์ต๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก push์ present๊ฐ ์์ต๋๋ค.
๐ push
push๋ ํ๋ฉด์ด ์์ด๋ ๋ฐฉ์์ผ๋ก ๋์์ ํฉ๋๋ค. ๊ทธ๋์ ์์ฐ์ค๋ฝ๊ฒ navigation์์ญ์ back์ด๋ผ๋ ๋ฒํผ์ด ์๊ธฐ๊ฒ ๋ฉ๋๋ค.
์คํ ๋ฆฌ ๋ณด๋
์ผ๋จ ์คํ ๋ฆฌ๋ณด๋์์ ์ด๋ป๊ฒ ํ๋์ง ์์๋ณผ๊น์?
๋จผ์ button์ ํ๋ ์์ฑํด์ค๋ค ์ค๋ฅธ์ชฝ ํด๋ฆญ์ผ๋ก ์ํ๋ view์ ์ ํ ๋ถ๋ถ ๊น์ง ๋๋ ๊ทธ๋ฅผ ํด์ค๋๋ค. ๊ทธ๋ผ show๋ผ๋ ์์ญ์ ์ ํ์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค. ๊ทธ๋ผ ์๋์ ํ๋ฉด๊ณผ ๊ฐ์ด ์ด๋ฐ ์์ด์ฝ์ด ์๊ธฐ๊ฒ ๋ฉ๋๋ค.

์ฝ๋
๊ทธ๋ผ ์ฝ๋๋ก๋ ์ด๋ป๊ฒ ํด์ผํ ๊น์?
@IBAction func tabCodePushButton(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(withIdentifier: "CodePushViewController") else { return }
self.navigationController?.pushViewController(viewController, animated: true)
}
navigatorController์์ pushViewController๋ฅผ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
๐ present
present๋ ํ๋ฉด์ ๋ฎ๋ ํํ๋ก ์์ฑ์ด ๋ฉ๋๋ค. ๊ทธ๋์ back ๋ฒํผ์ push์๋ ๋ค๋ฅด๊ฒ ์์ฑ์ด ๋์ง ์์ต๋๋ค.
์คํ ๋ฆฌ๋ณด๋
push์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋๋ ๊ทธ๋ฅผ ํด์ฃผ๋ฉด ๋ฉ๋๋ค. ์ด๋ presentModally๋ฅผ ์ ํํด์ฃผ๋ฉด ์ด๋ฐ ์์ด์ฝ์ด ์ฐ๊ฒฐ์ด ๋ฉ๋๋ค.

์ด๋ Storyboard์ Segue์ Presentation์ Full Screen ์ผ๋ก ํ์๋ฅผ ํด์ฃผ๋ฉด ํ ์คํฌ๋ฆฐ์ผ๋ก ๋ํ๋๊ฒ ๋ฉ๋๋ค.
์ฝ๋
์ฝ๋๋ก๋ ์ด๋ ๊ฒ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
@IBAction func tabCodePresentButton(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(withIdentifier: "CodePresentViewController") else { return }
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true, completion: nil)
}
์ฒ์์๋ ํ์คํฌ๋ฆฐ์ด ์๋๊ฒ ๋ ํ ๋ฐ ์ด๊ฒ์ modalPresentationStyle์ ๋ณ๊ฒฝ์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
๐ Back
์ ๊ทธ๋ผ ์ด์ ๋๋์๊ฐ๋ ๊ธฐ๋ฅ์ ์์์ผ๊ฒ ์ฃ ?
push ์์์ back
@IBAction func tabBackButton(_ sender: UIButton) {
// self.navigationController?.popViewController(animated: true) // ๋ฐ๋ก ์ ์ผ๋ก ๋์๊ฐ
self.navigationController?.popToRootViewController(animated: true) // root๋ก ๋์๊ฐ
}
present ์์์ back
@IBAction func tabBackButton(_ sender: UIButton) {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}