_/Velog
[iOS] 화면전환의 종류 (push vs present)
선달
2023. 12. 22. 16:54
반응형
push vs present
present | push | |
---|---|---|
전개방향 | 세로 | 가로 |
돌아가기 | dismiss | pop |
메소드 | UIViewController | UINavigationController |
🌪 UIViewController
1. present : 이동
- modally : 뷰 위에 뷰가 한겹 올라간 구조
- 세로방향으로 전개
- ex. alert 알람, 새로운 이메일 작성하기 등
@IBAction func 함수명(_ sender: Any) {
guard let 이동할뷰컨 = self.storyboard?.instantiateViewController(withIdentifier: "뷰컨아이디") else {return}
self.present(이동할뷰컨, animated: true, completion: nil)
}
present(_:animated:completion:)
viewControllerPresent
: 현재의 뷰컨 내용 위에 보여지는 뷰컨flag
: 애니메이션 여부completion
: 이동이 끝난 후 실행될 블록 =nil (리턴값, 파라미터 없음)
2. dismiss : 돌아가기
뷰컨을 네비게이션으로 부터 pop하고 화면 업데이트
@IBAction func 함수명(_ sender: Any) {
self.presentingViewController?.dismiss(animated: true)
}
dismiss(animated:)
animated
: 애니메이션 여부
+) 화면 꽉채우기
이동할뷰컨.modalPresentationStyle = .fullScreen
🌪 UINavigationController
1. push : 이동
- 스택 위에 뷰컨을 push하고 화면을 업데이트
- 가로방향으로 전개
@IBAction func 함수명(_ sender: Any) {
guard let 이동할뷰컨 = self.storyboard?.instantiateViewController(withIdentifier: "뷰컨아이디") else {return}
self.navigationController?.pushViewController(이동할뷰컨, animated: true
}
pushViewController(_:animated:)
viewController
: 스택 위에 푸쉬한 뷰컨, 탭바 컨트롤러는 불가능, 뷰컨이 이미 네비게이션 스택에 있을경우, 예외처리animated
: 애니메이션 여부
2. pop : 돌아가기
@IBAction func 함수명(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
popViewController(animated:)
animated
: 애니메이션 여부
3. popToRoot : 맨 처음으로
@IBAction func 함수명(_ sender: Any) {
self.navigationController?.popToRootViewController(animated: true)
}
popToRootViewController(animated:)
animated
: 애니메이션 여부
🤔 present는 popToRoot가 없나?
방법1 : present 된 컨뷰는 pop 불가능, dismiss 만 가능
dismiss를 하고난 뒤에 현재뷰컨 (네비게이션컨) 에서 popToRoot
--> `dismiss { pvc(현재뷰컨) pop }`
@IBAction func touchUpToGoToLoginView(_ sender: Any) {
guard let pvc = presentingViewController as? UINavigationController
else { return }
dismiss(animated: true) {
pvc.popToRootViewController(animated: false) }
}
방법2 : Unwind 세그웨이
https://minominodomino.github.io/devlog/2019/06/01/ios-UnwindSegueAndCustomSegue/
반응형