You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 19, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: _drafts/2020-06-08-expose-uikit-to-swiftui.md
+13-3Lines changed: 13 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ In this example we will create a simple app that will let the user select a docu
35
35
-`makeUIView(context: Self.Context) -> Self.UIViewType`, a method where you create the UIKit to be used in SwiftUI
36
36
-`updateUIView(_ uiView: Self.UIViewType, context: Self.Context)` a method called when the view must be redrawn due to external changes (e.g. a State update)
37
37
38
-
The `Self.UIViewType` type is an [associated type](https://www.hackingwithswift.com/articles/74/understanding-protocol-associated-types-and-their-constraints"swift protocol associated type") of the protocol and he must match the type of the UIKit view wrapped.
38
+
The `Self.UIViewType` type is an [associated type](https://www.hackingwithswift.com/articles/74/understanding-protocol-associated-types-and-their-constraints"swift protocol associated type") of the protocol and he must match the type of the UIKit view wrapped.
39
39
So for our example we can implement a `DocumentNameLabel` struct that implements the `UIViewRepresentable` protocol. In the `makeUIView` method we create an instance of the customized UILabel that we want to expose. In the `updateUIView` we will update the text shown by the label with the value contained in a `@Binding` var updated from the container view (do you remember [what is a @Binding var](https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-binding-property-wrapper"binding swiftui"), right?).
After the view, we need to take care of the UIKit controller used to select the document, `UIDocumentPickerViewController`. We will wrap it in a struct that implements the `UIViewControllerRepresentable`. ...
59
+
After the view, we need to take care of the UIKit controller used to select the document, `UIDocumentPickerViewController`. We will wrap it in a struct that implements the `UIViewControllerRepresentable`. This protocol contains four methods:
60
+
61
+
-`makeUIViewController(context: Self.Context) -> Self.UIViewControllerType`, a method used to create the instance of the view controller to be used in your SwiftUI screen
62
+
-`updateUIViewController(Self.UIViewControllerType, context: Self.Context)`, a method called when the view controller must be redrawn/updated due to external changes (e.g. a State update)
63
+
-`makeCoordinator() -> Self.Coordinator`, a method used to create the custom instance that you use to communicate changes from your view controller to other parts of your SwiftUI interface
64
+
-`static func dismantleUIViewController(Self.UIViewControllerType, coordinator: Self.Coordinator)`, a method called to clean up additional resources used by the view controller when it is dismissed
65
+
66
+
As for the previous protocol, The `Self.UIViewControllerType` type is an [associated type](https://www.hackingwithswift.com/articles/74/understanding-protocol-associated-types-and-their-constraints"swift protocol associated type") of the protocol and he must match the type of the UIViewController wrapped.
67
+
Le't start from the first method reported above, where we will create the instance of the `UIDocumentPickerViewController` that we want to expose to SwiftUI. The delegate of this controller will be the `Coordinator` instance created in the `makeCoodinator` protocol method. So the Coordinator will be responder for all the `UIDocumentPickerDelegate` methods. Remember that the `makeCoordinator` method is called before everything else when we will create our `DocumentPickerViewController` instance in SwiftUI. The `Coordinator` receive a reference to the wrapping struct. This reference is used to trigger a custom callback contained in the `var callback: (URL) -> ()` property that is received at `DocumentPickerViewController` construction time when the `documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])` method of the `UIDocumentPickerDelegate` protocol is invoked. In this way we are able to send back to SwiftUI the result of the interaction with the `UIDocumentPickerViewController`. Last but not least there's also the `updateUIViewController` method, but in this case with an empty implementation because we don't need to update the status of the view controller using some SwiftUI state change.
0 commit comments