[Swift] 모달 닫는 방식에 대한 고찰

유경박·2023년 8월 23일
3

Swift에서 화면전환에 자주 사용하는 Push와 더불어 모달도 자주 사용하고 있는데 이 Modal방식은 네비게이션이 없기때문에 모달을 여러개 띄울 경우 한번에 닫을수 없다.(push로 계속 했을 경우 pop으로 계속하여 닫을 수 있다.)

닫는방법이야 여러개 있겠지만(NotificationCenter, Delegate, Closure 등) 간단하게 모달로 된 다이얼로그들을 한번에 모두 닫게하는 내장 함수가 존재한다.

그것이 바로 presentingViewController.dismiss이다.

공식문서에는 아래와 같이 나와 있다.

When you present a view controller modally (either explicitly or implicitly) using the present(_:animated:completion:) method, the view controller that was presented has this property set to the view controller that presented it. If the view controller was not presented modally, but one of its ancestors was, this property contains the view controller that presented the ancestor. If neither the current view controller or any of its ancestors were presented modally, the value in this property is nil.

요약하자면 명시적이든 묵시적이든 VC.Present형태로 Modal로 띄어진 다이얼로그 한정하여, 현재 보여지고 있는 화면을 Presenting으로 처리하여 닫는다
*단, 모달형식으로 띄운게 없으면 nil을 반환하고 뷰 컨트롤러가 모달로 표시되지 않았지만 그 조상 중 하나가 모달로 표시되었다면 해당 모달을 반환한다.

profile
으아아

4개의 댓글

comment-user-thumbnail
2023년 8월 23일

여러개의 모달을 한번에 닫는 함수가 있다는 건 처음 알았네요!!! 좋은 글 감사합니다 :)

답글 달기
comment-user-thumbnail
2023년 8월 23일

우와 이런 재미난거 많이 올려주세요

답글 달기
comment-user-thumbnail
2023년 8월 23일

모달로 열면 계속 모달로 열려서 모달 대잔치였는데 꿀팁 알아갑니다!!!!

답글 달기
comment-user-thumbnail
2023년 8월 24일

꿀팁이네용 잘보고갑니다 !

답글 달기