본문 바로가기
iOS/흡구오디

[흡구오디] 흡연구역 등록하기 | 지도 위치 넘겨주기, 첫 화면으로 돌아가기

by 23g 2025. 2. 14.

첫번째 지도 화면의 위도경도 값을 -> 다음 지도 화면으로 넘겨주기

//잘못된 코드
@IBAction func confirmLocationTapped(_ sender: UIButton) {
        
        let addSmokingAreaDataVC = AddSmokingAreaDataViewController()
        
        let currentCenter = naverMapView.mapView.cameraPosition.target
        addSmokingAreaDataVC.latitude = currentCenter.lat
        addSmokingAreaDataVC.longitude = currentCenter.lng
        
        guard let smokingVC = storyboard?.instantiateViewController(withIdentifier: "AddSADataVC") as? AddSmokingAreaDataViewController else {
            print("SmokingArea 뷰컨 생성 실패")
            return
        }
        smokingVC.modalPresentationStyle = .fullScreen
        self.present(smokingVC, animated: true, completion: nil)
    }

 

//개선 코드
@IBAction func confirmLocationTapped(_ sender: UIButton) {
    guard let smokingVC = storyboard?.instantiateViewController(withIdentifier: "AddSADataVC") as? AddSmokingAreaDataViewController else {
        print("SmokingArea 뷰컨 생성 실패")
        return
    }
    
    let currentCenter = naverMapView.mapView.cameraPosition.target
    smokingVC.latitude = currentCenter.lat
    smokingVC.longitude = currentCenter.lng

    smokingVC.modalPresentationStyle = .fullScreen
    self.present(smokingVC, animated: true, completion: nil)
}

이미 smokingVC를 만들어놨기 때문에

smokingVC를 이용해서 lat, lng에 접근

-> 현재 위도경도 값을 전달함

 

전날 공부했던 화면 이동과데이터 전달 강의가 큰 도움이 되었다! 

 

흡연 구역 최종 등록 시 첫화면으로 돌아가기

//기존 코드
self.showAlert(message: "새로운 흡연구역이 등록되었습니다!") {
                            let viewController = self.storyboard?.instantiateViewController(withIdentifier: "VC") as? ViewController
                        }

원래는 instantiateViewController를 사용해서 홈화면으로 돌아가기만 하고

데이터가 제대로 표시되지 않는 등 문제가 있었지만

//개선 코드
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
                              // NotificationCenter로 ViewController에게 데이터 새로고침 요청
                              NotificationCenter.default.post(name: .smokingAreaAdded, object: nil)
                }

이렇게 rootViewController로 돌아가게했더니 아주 잘 동작한다!

 

무럭무럭 자라고 있는 나의 데이터들,,,,히히