-
[iOS] dismissBlock이라는 프로퍼티를 이용해보자.앱등이에게 살충제를 뿌린다./iOS 2016. 8. 13. 00:59
<제발 할리퀸 코스프레좀 네이버 다음 구글에서 단속해줬으면 좋겠다..>
FirstViewController에서 ModalViewController를 띄운다.
SecondViewController에서 ModalViewController를 띄운다.
ThirdViewController에서 ModalViewController를 띄운다.
세 개의 ViewController에서 다른 ViewController를 Modal로 띄우는 경우가 있다.
그리고 Modal이 닫겼을 때 특정 기능이 실행되길 원한다.
하지만 세 경우 모두 그 기능이 모두 다르다면?
ModalViewController에서 dismissviewcontrolleranimated completion메소드의 completion블락에 이 기능을 전달한다고 가정하자.
completion안에서 if 문을 통해 어떤 기능을 실행할지 분기태운다면 ModalViewController가 지저분해질 수 있다.
그래서 ModalViewController에 dismissBlock이라는 클로져 프로퍼티를 선언할 것이다.
예제소스 및 시나리오는 아래와 같다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersclass FirstViewController:UIViewController { func presentModalViewController() { let vc = ModalViewController() vc.dismissBlock = { print("원하는 기능1") } presentViewController(vc, animated: true, completion: nil) } } class SecondViewController:UIViewController { func presentModalViewController() { let vc = ModalViewController() vc.dismissBlock = { print("원하는 기능2") } presentViewController(vc, animated: true, completion: nil) } } class ModalViewController:UIViewController { var dismissBlock: (()->())? = nil override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) dismissBlock?() } } 이해하기 쉽고 유입도 안될 포스트같으니까.. 대충 여기서 끝!
'앱등이에게 살충제를 뿌린다. > iOS' 카테고리의 다른 글
[iOS] 아이패드(iPad)에서 UIActivityViewController를 Present하면 크래쉬가 발생. (0) 2016.09.20 [iOS] UIActivityCategory의 두 종류! Action과 Share (1) 2016.09.12 [iOS] UIButton의 중복터치를 막아라 (0) 2016.07.29 [iOS/UIToolbar] UIToolbar의 tintColor와 배경색backgroundColor (0) 2016.07.26 WWDC 2016 키노트 다시보기 (0) 2016.06.14