present(_:animated:completion:)란?
present(_:animated:completion:)란?
present는 UIViewController에서 다른 UIViewController를 표시하는 메서드로, 현재 화면에 새로운 뷰 컨트롤러를 모달 방식으로 표시할 때 사용됨.
1. 기본 문법
1
2
3
4
func present(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil)
매개변수 설명
viewControllerToPresent: 표시할 뷰 컨트롤러 객체 (UIViewController타입)animated: 화면 전환 애니메이션 사용 여부 (true: 애니메이션 적용,false: 즉시 표시)completion: 화면 전환이 완료된 후 실행할 클로저 (옵셔널, 기본값nil)
2. 사용 예제
예제 1: 기본적인 present 사용
1
2
3
let newVC = NewViewController()
present(newVC, animated: true, completion: nil)
newVC를 현재 뷰 컨트롤러에서 모달 방식으로 표시함.animated: true로 설정하면 화면 전환 애니메이션이 적용됨.
예제 2: 네비게이션 컨트롤러와 함께 사용
1
2
3
4
let newVC = NewViewController()
let navController = UINavigationController(rootViewController: newVC)
present(navController, animated: true, completion: nil)
UINavigationController를 포함한 형태로present하면, 모달 화면 내에서 네비게이션 기능을 사용할 수 있음.
예제 3: completion 클로저 활용
1
2
3
4
5
let newVC = NewViewController()
present(newVC, animated: true) {
print("화면 전환 완료")
}
- 화면 전환이 끝난 후 추가 작업을 수행할 때 사용.
3. push와 present의 차이
| 구분 | present | push |
|---|---|---|
| 사용 방식 | 모달 방식 | 네비게이션 스택 방식 |
| 필요 조건 | UIViewController에서 직접 호출 가능 | UINavigationController 내부에서만 사용 가능 |
| 화면 이동 | 독립적인 새로운 화면 | 이전 화면이 스택에 남아 있음 |
| 사용 예 | 로그인 화면, 설정 화면 | 앱 내 페이지 이동 |
예제: pushViewController와 비교
1
2
3
4
5
6
7
// present 방식 (모달)
let newVC = NewViewController()
present(newVC, animated: true, completion: nil)
// push 방식 (네비게이션)
let newVC = NewViewController()
navigationController?.pushViewController(newVC, animated: true)
present는UIViewController에서 바로 호출 가능하지만,pushViewController는UINavigationController내에서만 동작함.
4. present를 사용할 수 없는 경우
UIView,NSObject등UIViewController가 아닌 클래스에서present호출 시 “Cannot find ‘present’ in scope” 오류 발생.- 해결 방법:
UIViewController내부에서 호출하거나,UIViewController를weak var로 참조하여present를 호출해야 함.
결론
present는 현재 뷰 컨트롤러에서 새로운 화면을 모달 방식으로 표시할 때 사용.- 네비게이션이 필요한 경우
UINavigationController를 포함한 상태에서present가능. UIViewController외부에서는present호출이 불가능하므로,UIViewController참조를 받아야 함.
This post is licensed under CC BY 4.0 by the author.