Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update not working? #2

Closed
hfossli opened this issue Feb 4, 2021 · 1 comment · Fixed by #6
Closed

Update not working? #2

hfossli opened this issue Feb 4, 2021 · 1 comment · Fixed by #6

Comments

@hfossli
Copy link

hfossli commented Feb 4, 2021

struct ContentView: View {
    @State var counter = 0
    
    var body: some View {
        VStack {
            
            Text("Hello no \(counter) from SwiftUI")
                .padding()
            
            UILabel() // <- This can be any `UIKit` view.
                .swiftUIView(layout: .intrinsic) // <- This is returning a SwiftUI `View`.
                .set(\.text, to: "Hello no \(self.counter) from UIKit") // <- Use key paths for updates.
                .fixedSize()
        }
        .onAppear {
            if counter == 0 {
                schedule()
            }
        }
    }
    
    func schedule() {
        counter += 1
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.schedule()
        }
    }
}

simulator_screenshot_581B71A7-9A1F-45C6-8F09-2E1AB04392C5

I expected the UILabel to be updated and behave like my SwiftUI Text-view, but instead it is showing just 0. Also, recreating the UILabel every time the view hierarchy is declared is not a good idea as it may happen many many times. I think this is a fundamental issue with the approach in this library. I find this approach better and easier

import SwiftUI
import UIKit

struct UIViewMaker<ViewType: UIView>: UIViewRepresentable {
    
    typealias UIViewType = ViewType
    
    var make: () -> ViewType = ViewType.init
    var update: (ViewType) -> ()
    
    func makeUIView(context: Context) -> ViewType {
        return make()
    }
    
    func updateUIView(_ uiView: ViewType, context: Context) {
        update(uiView)
    }
}

struct ContentView: View {
    @State var counter = 0
    
    var body: some View {
        VStack {
            
            Text("Hello no \(counter) from SwiftUI")
                .padding()
            
            
            UIViewMaker<UILabel> {
                $0.text = "Hello no \(self.counter) from UIKit"
            }
            .fixedSize()
            
        }
        .onAppear {
            if counter == 0 {
                schedule()
            }
        }
    }
    
    func schedule() {
        counter += 1
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.schedule()
        }
    }

}
@AvdLee
Copy link
Owner

AvdLee commented Feb 4, 2021

Great find! I think I didn't encounter this earlier because I'm mainly using it for previews myself that come with static data.

Linking the thread for future reference: https://twitter.com/steipete/status/1354714371017089031?s=20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants