티스토리 뷰

안녕하세요!

Fury입니다 :)



바로 본론으로 갈게요! ㅎㅎ

 

먼저,

A ViewController -> B ViewController로 present를 했다고 가정할게요.

B ViewController에서 작업을 수행하고

dismiss를 합니다.

 

그리고 나를 띄운 A ViewController에서

무언가 처리를 해주어야 할 때!!!!!

 

방법은 여러가지가 있겠죠?

Delegate를 쓴다거나

NotificationCenter를 통해 Observing을 한다던가

나를 띄운 VC인 PresentingViewController를 찾는다던가!!

 

이 글에서는

PresentingViewController를 찾아서 작업을 하는 것을 설명드리려고요.

 

특히!!

PresentingViewController가 단순한 ViewController가 아니라면??

 

한 번 알아보죠

 

UITabBar -> UINavigationController -> ViewController의 PresentingViewController

 

만약 보통의 presentingViewController 찾는 코드라면

 

if let pvc = presentingViewController as? ViewController {
	pvc.alertNotice()
}

 

이런 식으로 코드를 작성하겠죠.

 

하지만, presentingVC가

UITabBarController안에 UINavigationController안에 속해있는 ViewController라면??

 

presentingViewController를 출력해보면

UITabBarController가 나오는 것을 확인할 수 있을 거예요.

 

그래서 결론을 내자면

아래와 같이

찾으시면 됩니다!!

 

if let tvc = self.presentingViewController as? UITabBarController {
	if let nvc = tvc.selectedViewController as? UINavigationController {
		if let pvc = nvc.topViewController as? ViewController {
			let chatRoomVC = ChatRoomViewController()
			chatRoomVC.observerCount = -1
			chatRoomVC.modalPresentationStyle = .fullScreen
			self.dismiss(animated: false) {
				pvc.goToChatRoom(groupDB)
			}
		}
	}
}

 

UITabBarController -> UINavigationController -> ViewController 순으로 찾으시면 됩니다!

 

그냥 삽질하다가 정리 한 번 해봤습니다ㅋㅋㅋ

 

감사합니당 :)

 

 

댓글