1

I made a simple application. I made a subclass of a UIView which presents a UIButton. Whenever I tap the button, the value of the "number" property increases by 1. I integrated this custom UIView in a SwiftUI View by the help of the UIViewRepresentable protocol. How can I access the "number" property in the SwiftUI View?

import UIKit

class CustomUIView: UIView {
    var number = 0

    override init(frame:CGRect) {
        super.init(frame: frame)
        createButton()
    }

    required init?(coder: NSCoder) {
        fatalError("error")
    }

    private func createButton () {
        let button = UIButton();
        button.setTitle("Add", for: .normal)
        button.setTitleColor(UIColor.blue, for: .normal)
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        self.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }

    @objc func buttonTapped(sender: UIButton) {
        number += 1
        print(number)
    }
}
import SwiftUI

struct CustomButton: UIViewRepresentable {
    func makeUIView(context: Context) -> CustomUIView {
        let customButton = CustomUIView()
        return customButton
    }

    func updateUIView(_ view: CustomUIView, context: Context) {

    }
}

struct ContentView : View {
    var body: some View {
        NavigationView {
            Text("I want to show here the value of the number property")
            CustomButton().frame(height: 50)
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Maric Vikike
  • 237
  • 2
  • 8

1 Answers1

2

I would recommend using a Binding in your custom view so that the SwiftUI view is still the source of truth of value.

class CustomUIView: UIView {
    var number: Binding<Int>!

    override init(frame:CGRect) {
        super.init(frame: frame)
        createButton()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        createButton()
    }

    private func createButton () {
        let button = UIButton();
        button.setTitle("Add", for: .normal)
        button.setTitleColor(UIColor.blue, for: .normal)
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        self.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }

    @objc func buttonTapped(sender: UIButton) {
         number.value += 1
    }
}
struct CustomButton: UIViewRepresentable {
    var binding: Binding<Int>

    init(number: Binding<Int>) {
        self.binding = number
    }
    func makeUIView(context: Context) -> CustomUIView {
        let customButton = CustomUIView()
        customButton.number = binding
        return customButton
    }

    func updateUIView(_ view: CustomUIView, context: Context) {

    }
}

struct ContentView : View {
    @State var number = 0

    var body: some View {
        NavigationView {
            Text("I want to show here the value of the number property")
            .lineLimit(nil)
            Text("Current value: \(number)")
            CustomButton(number: $number).frame(height: 50)
        }
    }
}
rraphael
  • 10,041
  • 2
  • 25
  • 33