iOS Using UIKit on SwiftUI with delegate

운영 체제: macOS Ventura 13.0
사용 버전: Xcode 14.0.1, SwiftUI, Swift, UIKit

iOS Using UIKit on SwiftUI with delegate를 알아보겠습니다.

UIKit을 사용할 때에는 Delegate가 자주 사용됩니다. 어떻게 SwiftUI에서 UIKit을 사용할 때 delegate를 설정하는지 알아봅시다.

이전 iOS Using UIKit on SwiftUI 게시글에 이어서 진행합니다.
https://shwoghk14.blogspot.com/2022/11/ios-using-uikit-on-swiftui.html


UITextVIew에는 UITextViewDelegate가 사용됩니다.


MyUITextView 내부에 class Coordinator라는 클래스를 하나 만듭니다.

NSObject와 UITextViewDelegate를 상속받습니다.

parent 타입에는 밖의 struct의 이름을 적고 init을 만들어서 parent를 초기화합니다.

저의 경우 parent의 타입은 MyUITextView입니다.







Coordinator는 UIView와 SwiftUI와의 상호 작용에 사용되기도 하고 지금같이 Delegate나 DataSource 등을 위해서도 사용됩니다. parent에 밖의 struct를 연결함으로써 Coordinator와 MyUITextView가 서로 상호작용할 수 있습니다.


밖의 struct에 13 번째 줄처럼 makeCoordinator 함수를 만듭니다.

makeUIView 함수보다 makeCoordinator 함수가 더 빨리 불러와집니다.








textViewDidBeginEditing이라는 UITextViewDelegate 중의 하나의 함수를 사용해 보겠습니다.

36 번째 줄처럼 delegate 함수를 추가합니다. 편집이 시작될 때 동작하는데, 미리 Hello라는 글자가 적히도록 만들었습니다.






20 번째 줄처럼 delegate에는 context.coordinator를 선언합니다.





실행해 봅시다.


짜잔.







끝.


카테고리: iOS

댓글

이 블로그의 인기 게시물

Python OpenCV 빈 화면 만들기

Python urllib.parse.quote()

Python bytes.fromhex()

Android AVD Ram size change

Forensics .pyc 파일 .py로 복구하기

Android Minimum touch target size

KiCad 시작하기 7 (FreeRoute 사용하기 2)

Android Notification with Full Screen

C++ OpenCV 모폴로지 침식, 팽창

KiCad 시작하기 2 (PCB 만들기)