Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swiftui 快速用户界面更改显示在操作表中的状态_Swiftui_Uiviewrepresentable - Fatal编程技术网

Swiftui 快速用户界面更改显示在操作表中的状态

Swiftui 快速用户界面更改显示在操作表中的状态,swiftui,uiviewrepresentable,Swiftui,Uiviewrepresentable,我有一个简单的SwiftUI示例,它的行为不符合预期 我期望发生的事情: 单击按钮时,工作表应显示一个随机数(text) 发生了什么:第一次,工作表显示foo。在随后的时间,会出现一个数字 为什么第一次没有出现数字? ContentView.swift PillButton.swift 导入快捷界面 结构PillButton:UIViewRepresentable{ 标题:字符串 让动作:()->() @绑定变量文本:字符串 @绑定变量展示表:Bool var pillButton=UIBut

我有一个简单的SwiftUI示例,它的行为不符合预期

我期望发生的事情: 单击按钮时,工作表应显示一个随机数(
text

发生了什么:第一次,工作表显示
foo
。在随后的时间,会出现一个数字

为什么第一次没有出现数字?


ContentView.swift PillButton.swift
导入快捷界面
结构PillButton:UIViewRepresentable{
标题:字符串
让动作:()->()
@绑定变量文本:字符串
@绑定变量展示表:Bool
var pillButton=UIButton()
func makeCoordinator()->Coordinator{Coordinator(self)}
类协调器:NSObject{
变量父项:PillButton
初始化(uPillButton:pillButton){
self.parent=pillButton
super.init()
}
@objc func doAction(\发送方:任何){
设number=Int.random(in:0..ui)按钮{
let button=ui按钮(类型:。系统)
button.setTitle(self.title,用于:。正常)
button.addTarget(context.coordinator,action:#选择器(coordinator.doAction(:)),for:。着地)
返回按钮
}
func updateUIView(uiView:ui按钮,context:context){}
}
使用ViewModel(ObservableObject)类。在本例中,struct view捕获了初始值。因此,请使用ObservableObject类引用该值

这是可能的解决办法

class ContentViewModel: ObservableObject{
    @Published var text = "foo"
}
struct ContentView: View {
    @State var showSheet = false
    @ObservedObject var viewModel = ContentViewModel()
    
    var body: some View {
        VStack {
            Text("Hello")
            PillButton(title: "Start Test", action: {
                print("Button tapped")
            }, text: $viewModel.text, showSheet: $showSheet)
        }.sheet(isPresented: $showSheet, content: {
            Text(self.viewModel.text)
        })
    }
}

谢谢!这很管用。你能解释一下为什么其他行为不能像我预期的那样管用吗?我发现这本指南有助于澄清一些困惑:
import SwiftUI

struct PillButton: UIViewRepresentable {
    let title: String
    let action: () -> ()
    @Binding var text: String
    @Binding var showSheet: Bool

    var pillButton = UIButton()

    func makeCoordinator() -> Coordinator { Coordinator(self) }

    class Coordinator: NSObject {
        var parent: PillButton

        init(_ pillButton: PillButton) {
            self.parent = pillButton
            super.init()
        }

        @objc func doAction(_ sender: Any) {
            let number = Int.random(in: 0..<100)
            self.parent.text = String(number)
            self.parent.action()
            self.parent.showSheet = true
        }
    }

    func makeUIView(context: Context) -> UIButton {
        let button = UIButton(type: .system)
        button.setTitle(self.title, for: .normal)
        button.addTarget(context.coordinator, action: #selector(Coordinator.doAction(_ :)), for: .touchDown)
        return button
    }

    func updateUIView(_ uiView: UIButton, context: Context) {}
}

class ContentViewModel: ObservableObject{
    @Published var text = "foo"
}
struct ContentView: View {
    @State var showSheet = false
    @ObservedObject var viewModel = ContentViewModel()
    
    var body: some View {
        VStack {
            Text("Hello")
            PillButton(title: "Start Test", action: {
                print("Button tapped")
            }, text: $viewModel.text, showSheet: $showSheet)
        }.sheet(isPresented: $showSheet, content: {
            Text(self.viewModel.text)
        })
    }
}