Present | Push | |
---|---|---|
뷰 전개 | 세로(아래에서 위) | 가로(왼쪽에서 오른쪽) |
방식 | 화면을 다른 화면 위로 띄워 표현하는 방식 | 새로운 화면을 viewControllers에 새롭게 추가시키고 topViewController로 설정 |
어느 class로 부터의 메소드 | UIViewController의 method | UINavigationController의 method |
예제 | alert 등 view 위에 다른 무언가 띄울때도 사용 | NavigationController를 통해 뭔가 이동하고 뒤로가고 할때 많이 사용 |
Present
Push
UIViewController | Apple Developer Documentation
UIViewController: UIKit 앱의 뷰 계층을 관리하는 객체입니다.
UIViewController
클래스의method
- 전환 방식:
세로
방향- modalPresentationStyle로 화면 전환이 되는 스타일을 정할 수 있음!
Presents a view controller modally.
func present(
_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil
)
@IBAction func present(_ sender: Any) {
let twoVC = self.storyboard?.instantiateViewController(withIdentifier: "twoViewController")
twoVC?.modalPresentationStyle = .fullScreen
self.present(twoVC!, animated: true, completion: nil)
}
present된 뷰를 메모리에서 삭제하고 걷어내기
func dismiss(
animated flag: Bool,
completion: (() -> Void)? = nil
)
@IBAction func dismiss(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
pushViewController(_:animated:) | Apple Developer Documentation
pushViewController: 계층적 콘텐츠를 탐색하기 위한 스택 기반 체계를 정의하는 컨테이너 뷰 컨트롤러
pushViewController
의method
- 전환 방식:
가로
방향- 스택에 뷰를 쌓는 형태로 화면을 업데이트
func pushViewController(
_ viewController: UIViewController,
animated: Bool
)
@IBAction func push(_ sender: Any) {
let twoVC = self.storyboard?.instantiateViewController(withIdentifier: "twoViewController")
self.navigationController?.pushViewController(twoVC!, animated: true)
}
스택에 쌓인 뷰 1개를 pop하고 돌아가기
func popViewController(animated: Bool) -> UIViewController?
@IBAction func pop(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
func popToViewController(
_ viewController: UIViewController,
animated: Bool
) -> [UIViewController]?
@IBAction func gotoNextVC(_ sender: Any)
guard let signUpVC = self.storyboard?.instantiateViewController(withIdentifier: "SignUpNickNameVC") else {return}
welcomeVC.modalPresentationStyle = .fullScreen
self.present(welcomeVC, animated: true, completion: {
self.navigationController?.popToRootViewController(animated: true)
})
}
func popToViewController(
_ viewController: UIViewController,
animated: Bool
) -> [UIViewController]?
@IBAction func gotoBack(_ sender: Any)
guard let signUpVC = self.storyboard?.instantiateViewController(withIdentifier: "SignUpNickNameVC") else {return}
welcomeVC.modalPresentationStyle = .fullScreen
self.present(welcomeVC, animated: true, completion: {
//navigation 스택에서 돌아가고싶은 뷰까지 pop하면서 이동
if let rootVC = ViewControoller as? LoginVC {
self.navigationController?.popToViewController(rootVC ,animated: true)
}
})
}
VC의 생명주기를 이용해서 어떤 부분이 불러지고 꺼지는지 아래 method를 통해 확인!
extension ViewController {
override func viewWillAppear(_ animated: Bool) {
print("mainViewController", #function)
super.viewWillAppear(animated)
}
override func viewIsAppearing(_ animated: Bool) {
print("mainViewController", #function)
super.viewIsAppearing(animated)
}
override func viewDidAppear(_ animated: Bool) {
print("mainViewController", #function)
super.viewDidAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
print("mainViewController", #function)
super.viewWillDisappear(animated)
}
override func viewDidDisappear(_ animated: Bool) {
print("mainViewController", #function)
super.viewDidDisappear(animated)
}
}
present(_:animated:completion:) | Apple Developer Documentation
pushViewController(_:animated:) | Apple Developer Documentation
iOS | present vs push
[iOS] 화면 전환 방식 push vs present
https://medium.com/@zzzzot/a-comprehensive-comparison-when-to-push-to-uinavigationcontroller-vs-when-to-present-modally-d9f54be2e11e