swift 选择时的选取器

oknwwptz  于 2023-02-18  发布在  Swift
关注(0)|答案(3)|浏览(121)

我试图在用户选择选取器选项后运行函数。
这个想法是用户可以设置一个默认站点,所以我需要能够发送选择的值到一个函数,这样我就可以把它保存在核心数据模块中,我该如何实现呢?

import SwiftUI

struct SettingsView: View {
    var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
    @State private var selectedFrameworkIndex = 0
    
    func doSomethingWith(value: String) {
        print(value)
    }
    
    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedFrameworkIndex, label: Text("Favorite Station")) {
                        ForEach(0 ..< frameworks.count) {
                            Text(self.frameworks[$0])
                        }
                    }.onReceive([self.frameworks].publisher.first()) { value in
                        self.doSomethingWith(value: value)
                    }
                }
            }
            .navigationBarTitle("Settings")
        }
    }
}
ygya80vv

ygya80vv1#

尝试使用onChange而不是onRecive
验证码:

.onReceive([self.frameworks].publisher.first()) { value in
    self.doSomethingWith(value: value)
}

更正:

.onChange(of: $selectedFrameworkIndex, perform: { value in
        self.doSomethingWith(value: value)
    })

这将在每次$selectedFrameworkIndex改变时触发。

643ylb08

643ylb082#

我将在picker上使用一个简单的.onChange(of:)调用,它将拾取@State var的更改,并允许您对其进行操作,如下所示:

import SwiftUI

struct SettingsView: View {
   var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
   @State private var selectedFrameworkIndex = 0

    
    func doSomethingWith(value: String) {
            print(value)
      }
    
   var body: some View {
    NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedFrameworkIndex, label: Text("Favorite Station")) {
                        ForEach(0 ..< frameworks.count) {
                            Text(self.frameworks[$0])
                        }
                    }
                     // Put your function call in here:
                     .onChange(of: selectedFrameworkIndex) { value in
                        print(value)
                    }

                    }
                }
            }
            .navigationBarTitle("Settings")
        }
   }

}
aamkag61

aamkag613#

你方:

import SwiftUI

struct SettingsView: View {
   var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
   @State private var selectedFrameworkIndex = 0
   
   func doSomethingWith(value: String) {
       print(value)
   }
    
   var body: some View {
    NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedFrameworkIndex, label: Text("Favorite Station")) {
                        ForEach(0 ..< frameworks.count) {
                            Text(self.frameworks[$0])
                        }
                    }.onReceive([self.frameworks].publisher.first()) { value in
                        self.doSomethingWith(value: value)
                    }
                }
            }
            .navigationBarTitle("Settings")
        }
   }

}

添加用于检查selectedFrameworkIndex是否已更改的变量。更改:

import SwiftUI

struct SettingsView: View {
    var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
    @State private var selectedFrameworkIndex = 0
    @State private var savedFrameworkIndex = 0
    
   func handler<Value: Any>(val: Value) -> Value {
       if selectedFrameworkIndex != savedFrameworkIndex {
           self.doSomethingWith(value: self.frameworks[selectedFrameworkIndex])
       }
       return val
   }

    func doSomethingWith(value: String) {
        print(value)
    }
    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: handler(val: $selectedFrameworkIndex), label: Text("Favorite Station")) {
                        ForEach(0 ..< frameworks.count) {
                            Text(self.frameworks[$0])
                        }
                    }
                }
            }
            .navigationBarTitle("Settings")
        }
    }
}

相关问题