Cannot find 'present' in scope 오류 발생
Cannot find 'present' in scope 오류 발생
1. 문제 발생 상황
present(_:animated:completion:)
메서드를 호출했을 때 “Cannot find ‘present’ in scope” 오류가 발생함.
2. 원인 분석
2.1 present
는 UIViewController
의 인스턴스 메서드
present
메서드는UIViewController
내부에서만 호출 가능함.UIView
,NSObject
, 또는 다른 클래스에서present
를 직접 호출하면 오류 발생.
2.2 handleTap()
메서드가 UIViewController
내부에 없음
handleTap()
이UIView
또는 다른 클래스에서 정의되어 있을 가능성이 높음.UIView
는present
메서드를 제공하지 않으므로 직접 호출할 수 없음.
3. 해결 방법
방법 1: UIViewController
내부에서 호출
handleTap()
이 UIViewController
내부에 있다면, 아래처럼 수정하면 정상적으로 동작함.
1
2
3
4
5
6
7
8
class MyViewController: UIViewController {
@objc func handleTap() {
let nodeDetailVC = NodeDetailViewController()
let navController = UINavigationController(rootViewController: nodeDetailVC)
navController.modalPresentationStyle = .fullScreen
present(navController, animated: true, completion: nil) // 오류 해결
}
}
방법 2: UIView
에서 UIViewController
참조를 받아 호출
UIView
에서present
를 호출하려면,UIViewController
를 참조해야 함.UIViewController
의 인스턴스를UIView
에 전달하고, 이를 통해present
호출.
1
2
3
4
5
6
7
8
9
10
11
class CustomView: UIView {
weak var viewController: UIViewController? // 뷰 컨트롤러 참조
@objc func handleTap() {
let nodeDetailVC = NodeDetailViewController()
let navController = UINavigationController(rootViewController: nodeDetailVC)
navController.modalPresentationStyle = .fullScreen
viewController?.present(navController, animated: true, completion: nil) // 오류 해결
}
}
방법 3: UIViewController
에서 UIView
에 참조 설정
UIViewController
에서UIView
의viewController
속성을 설정하여,UIView
가present
를 호출할 수 있도록 함.
1
2
3
4
5
6
7
8
9
10
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let customView = CustomView()
customView.viewController = self // 뷰 컨트롤러 참조 전달
view.addSubview(customView)
}
}
4. 결론
present
는UIViewController
에서만 호출 가능하므로,UIView
에서 호출할 경우UIViewController
참조를 받아야 함.UIViewController
에서 실행되는지 확인 후,UIView
에서 호출할 경우viewController?.present(...)
방식으로 해결 가능.
This post is licensed under CC BY 4.0 by the author.