swift 如何使文本字段在选择特定选择器值时自动更改

0lvr5msh  于 2023-04-04  发布在  Swift
关注(0)|答案(2)|浏览(120)

请告诉我如何使它,以便当从选择器字段中选择某个值时,文本字段中的结果自动更改。我写了代码,但它给出了一个错误类型'()'不能符合'视图'。下面是代码:

import SwiftUI

struct ContentView: View {
    
    @State private var years: [String] = ["2010", "2011", "2012"]
    @State private var selectedYearIndex = 2
    @State private var resultText = ""
    
    
    var body: some View {
        
        switch selectedYearIndex {
        case 0:
            self.resultText = "Hello"
        case 1:
            self.resultText = "Hi"
        case 2:
            self.resultText = "Bye"
        default:
            self.resultText = "No"
        }
        
        VStack {
            Text("Result Text:")
            Spacer()
            Text("\(resultText)")
            Spacer()
            
            Picker("Choose a Year", selection: $selectedYearIndex) {
                
                ForEach(0..<years.count) {
                    Text("\(years[$0])").font(.title)
                }
                
            }
            
        }
    }
}
vlju58qv

vlju58qv1#

我建议使用onChange,这正是它被创建的目的:

struct ContentView: View {
    
    @State private var years: [String] = ["2010", "2011", "2012"]
    @State private var selectedYearIndex = 2
    @State private var resultText = ""
    
    var body: some View {
        VStack {
            Text("Result Text:")
            Spacer()
            Text("\(resultText)")
            Spacer()
            
            Picker("Choose a Year", selection: $selectedYearIndex) {
                
                ForEach(0..<years.count) {
                    Text("\(years[$0])").font(.title)
                }
            }
            .onChange(of: selectedYearIndex) { index in // <-- what changed
                switch index { // <-- what to do when it changed
                case 0:
                    resultText = "Hello"
                case 1:
                    resultText = "Hi"
                case 2:
                    resultText = "Bye"
                default:
                    resultText = "No"
                }
            }
        }
    }
}

当然,你也可以将逻辑移动到一个单独的函数中:

// ...
            Picker("Choose a Year", selection: $selectedYearIndex) {
                
                ForEach(0..<years.count) {
                    Text("\(years[$0])").font(.title)
                }
            }
            .onChange(of: selectedYearIndex, perform: onChangeOfPicker)
        }
    }
    
    func onChangeOfPicker(index: Int) {
        switch index {
        case 0:
            resultText = "Hello"
        case 1:
            resultText = "Hi"
        case 2:
            resultText = "Bye"
        default:
            resultText = "No"
        }
    }
jvidinwx

jvidinwx2#

因为你试图在视图的主体中为resultText赋值。这是不允许的,因为视图的主体必须返回一个视图,而不是执行操作或赋值。
以下是代码的更新版本,其中包含以下更改:

import SwiftUI

struct ContentView: View {
    
    @State private var years: [String] = ["2010", "2011", "2012"]
    @State private var selectedYearIndex = 2
    @State private var resultText = ""
    
    var selectedYearText: String {
        switch selectedYearIndex {
        case 0:
            return "Hello"
        case 1:
            return "Hi"
        case 2:
            return "Bye"
        default:
            return "No"
        }
    }
    
    var body: some View {
        VStack {
            Text("Result Text:")
            Spacer()
            Text("\(selectedYearText)")
                .onReceive([self.selectedYearIndex].publisher.first()) { _ in
                    self.resultText = selectedYearText
                }
            Spacer()
            
            Picker("Choose a Year", selection: $selectedYearIndex) {
                ForEach(0..<years.count) {
                    Text("\(years[$0])").font(.title)
                }
            }
        }
    }
}

相关问题