[Xcode] Push segues can only be used when the source controller is managed by an instance of UINavigationController.

iOSoo·2021년 11월 28일
0

Push segues can only be used when the source controller is managed by an instance of UINavigationController.

위와 같은 에러가 떴을 땐 대부분의 문제는 UINavigationController를 연결하지 않아서 생기는 오류이다.

하지만 나는 위의 문제외의 다른 문제가 발생해서 생기는 오류의 해결법에 대해서 작성하겠다.

[문제]

현재 내 상황은 로그인VC에서 MainVC를 연결하고 MainVC에서 버튼을 클릭하면 Push로 화면 전환을 하도록 만들었다. (이때 MainVC는 UINavigationViewController와 연결되어 있다.)

하지만 Push를 코드를 작성하면 아무런 반응이 없었고 Segue로 연결하면 위의 오류가 떠서 App이 멈추게 된다.

[해결]

실패 Version

나는 로그인VC에서 MainVC로 아래의 코드로 화면전환을 했다.

let alStoryboard = UIStoryboard(name: "Main", bundle: nil) //스토리보드 결정
let alarmVC = alStoryboard.instantiateViewController(identifier: "MainViewController")
alarmVC.modalTransitionStyle = .coverVertical
alarmVC.modalPresentationStyle = .fullScreen
self.present(alarmVC, animated: true, completion: nil)

이렇게 InstantiateViewController로 MainVC를 띄우면 그냥 MainVC만 띄워지게 되어 UINavigationViewController를 포함하지 않게되어 에러가 뜨게된다.

성공 Version

let alStoryboard = UIStoryboard(name: "Main", bundle: nil) //스토리보드 결정
        let alarmVC = alStoryboard.instantiateInitialViewController()
        alarmVC?.modalTransitionStyle = .coverVertical
        alarmVC?.modalPresentationStyle = .fullScreen
        self.present(alarmVC!, animated: true, completion: nil)

이렇게 instantiateInitialViewController()로 화면을 전환하면 Initial인 네비(Main뷰컨이 있는)로 전환하게 된다.

profile
애플을 좋아하는 비전공생

0개의 댓글