250x250
KemiKIM
29 to iOS
KemiKIM
GitHub
Loading data ...
  • 분류 전체보기 (41)
    • 지금 : 세상에서 가장 간단한 메모 앱 (1)
    • iOS (22)
      • Swift (4)
      • Xcode, UI (7)
      • And (9)
      • 이것이 삽질이다. (2)
    • other (18)
      • Error Solution (8)
      • a Little Tip (5)
      • Mac Start Setting (5)
    • Project (0)
hELLO · Designed By 정상우.
KemiKIM

29 to iOS

present closure처럼 pushViewController로는 쓸 수 없는걸까?
iOS/Xcode, UI

present closure처럼 pushViewController로는 쓸 수 없는걸까?

2023. 1. 6. 15:03

(Macbook Air M1 8-256 Monterey 12.6.2 Xcode 13.4)

 

 

Why?

일반적으로 다른 ViewController로 넘어갈 때,

대표적으로 present(viewcontroller: , completion: ) method를 사용하여,

closure형태로 다른 ViewController에게 할 일을 시키거나 Data 전달하는 형태를 많이 볼 수 있습니다.

 

하지만 Navigation Controller에 존재하는

pushviewcontroller는 parameter가 ViewController와 animation밖에 없습니다.

 

그래서 navigation형태에서는

즉, " pushviewcontroller형태에서는 closure를 사용하여 다음 뷰컨에 Data를 전달할 수 없는걸까? "

라는 생각이 들어서 해당 내용을 찾아보았습니다.

 

 

 


 

 

 

해결방법

 

 

먼저 제가 참조한 링크를 게시해드립니다.

참조링크:  https://padamthapa.com/blog/adding-completion-block-for-popviewcontroller/

 

 

이 글쓴이는 저랑은 약간 다르지만, 결국 같은 생각에 의해 글을 기재한 것으로 보였습니다.

 

결론적으로 extension에 NavigationController를 custom해줌으로써

present closure처럼 해당 내용을 사용할 수 있게되었습니다.

 

 

 

 

(1) extension에 해당 code를 작성해줍니다.

extension UINavigationController {
    func popViewControllerWithHandler(animated:Bool = true, completion: @escaping ()->()) {
        CATransaction.begin()
        CATransaction.setCompletionBlock(completion)
        self.popViewController(animated: animated)
        CATransaction.commit()
    }

    func pushViewController(viewController: UIViewController, animated:Bool = true,  completion: @escaping ()->()) {
        CATransaction.begin()
        CATransaction.setCompletionBlock(completion)
        self.pushViewController(viewController, animated: animated)
        CATransaction.commit()
    }
}

 

CATransaction은

특정 animation이 종료된 후 completionBlock처리가 가능한 method입니다.

 

 

 

 

(2) 원하는 곳에서 사용하시면 끝!

// (1) push
self.navigationController?.pushViewController(viewController: controller,
                                              animated: true,
                                              completion: {
                                              
})

// or

self.navigationController?.pushViewController(viewController: controller, 
                                              animated: true) {
                                              
}




// (2) pop
self.navigationController?.popViewControllerWithHandler(animated: true,
                                                        completion: {
                                                        
})

// or

self.navigationController?.popViewControllerWithHandler(animated: true) {

}

 

 

 

 


 

 

 

 

궁금증도 해결하고,

새로운 개념인 CATransaction도 공부할 수 있어서 굉장히 신선했습니다.

 

 

필요한 분들에게 도움이 되셨으면 좋겠습니다.

 

오류가 있거나, 피드백이 있다면 언제든지 댓글로 알려주세요!

감사합니다 :)

'iOS > Xcode, UI' 카테고리의 다른 글

Notification(1) - NotificationCenter 사용하기  (0) 2023.01.22
Alert Controller 사용하기  (0) 2023.01.19
TableView Top(header) Padding 제거  (0) 2022.10.22
Protocol과 Delegate Pattern을 이용해 Data를 가져와 표시하는 화면 구성하기. +체크표시 활성화하기  (1) 2022.10.05
Protocol과 Delegate Pattern을 이용해 Data를 가져와 표시하는 화면 구성하기.  (1) 2022.09.21
    'iOS/Xcode, UI' 카테고리의 다른 글
    • Notification(1) - NotificationCenter 사용하기
    • Alert Controller 사용하기
    • TableView Top(header) Padding 제거
    • Protocol과 Delegate Pattern을 이용해 Data를 가져와 표시하는 화면 구성하기. +체크표시 활성화하기
    KemiKIM
    KemiKIM
    안녕하세요! iOS App Developer를 희망하는 주니어 개발자입니다.

    티스토리툴바