如何在 SwiftUI 视图中访问 UIView 子类的属性?

Posted

技术标签:

【中文标题】如何在 SwiftUI 视图中访问 UIView 子类的属性?【英文标题】:How can I access a property of a subclass of UIView in a SwiftUI View? 【发布时间】:2019-06-20 21:10:33 【问题描述】:

我做了一个简单的应用程序。我创建了一个 UIView 的子类,它提供了一个 UIButton。每当我点击按钮时,“数字”属性的值都会增加 1。我在 UIViewRepresentable 协议的帮助下将此自定义 UIView 集成到 SwiftUI 视图中。如何访问 SwiftUI 视图中的“数字”属性?

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)
        
    

【问题讨论】:

【参考方案1】:

我建议在您的自定义视图中使用 Binding,以便 SwiftUI 视图仍然是价值的真实来源。

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)
        
    

【讨论】:

以上是关于如何在 SwiftUI 视图中访问 UIView 子类的属性?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 SwiftUI 视图中包装 UIBarButtonItem?

在 SwiftUI 中更改 UIView 背景颜色

如何在最新的 SwiftUI App 中获取窗口或视图的所有子视图?

如何在 SwiftUI 中随时从子视图作为父视图访问数据?

在 UITableViewCell 内部的 UIView 上添加 SwiftUI [重复]

我们如何降低 Swift 中 UIView 的饱和度?