根据 SwiftUI 中的切换值更改文本
Change text based on toggle values in SwiftUI
我在 SwiftUI.When 中创建了一个带有文本、图像和导航按钮的视图,按下导航按钮它将导航到另一个包含 Toggle.When 的视图我更改了切换值,我也想更改上一个视图中的文本值。
更改开关时值正在更新,但在以前的视图中访问时未反映出来。
//BluetoothView.swift
struct BluetoothView: View {
@ObjectBinding var bluetooth = Settings()
var body: some View {
return NavigationButton(destination: ToggleBluetoothView()) {
HStack() {
Image("default")
.resizable()
.cornerRadius(12)
.frame(width: 25, height: 25)
.clipped()
.aspectRatio(contentMode: .fit)
Text("Bluetooth")
.color(.blue)
.font(.system(size: 18))
Text(bluetooth.isBluetoothOn ? "On" : "Off")
.color(.gray)
.font(.subheadline)
.frame(width: 50, height: 40, alignment: .trailing)
}
}
}
}
//切换BluetoothView.swift
struct ToggleBluetoothView: View {
@ObjectBinding var bluetooth = Settings()
var body: some View {
Form {
Section(header: Text("ENABLE TO CONNECT WITH NEARBY DEVICES")) {
Toggle(isOn: $bluetooth.isBluetoothOn) {
Text("Bluetooth")
}
}
}
}
}
//Settings.swift
class Settings: BindableObject {
var didChange = PassthroughSubject<Void, Never>()
var isBluetoothOn = false { didSet { update() } }
func update() {
didChange.send(())
}
}
您在每个视图中分别实例化设置。两个视图都需要看到相同的设置对象:
更改以下内容:
NavigationButton(destination: ToggleBluetoothView(bluetooth: bluetooth)) { ... }
并删除 ToggleBluetoothView 中的初始值:
struct ToggleBluetoothView: View {
@ObjectBinding var bluetooth: Settings
var body: some View {
Form {
Section(header: Text("ENABLE TO CONNECT WITH NEARBY DEVICES")) {
Toggle(isOn: $bluetooth.isBluetoothOn) {
Text("Bluetooth")
}
}
}
}
}
我在 SwiftUI.When 中创建了一个带有文本、图像和导航按钮的视图,按下导航按钮它将导航到另一个包含 Toggle.When 的视图我更改了切换值,我也想更改上一个视图中的文本值。
更改开关时值正在更新,但在以前的视图中访问时未反映出来。
//BluetoothView.swift
struct BluetoothView: View {
@ObjectBinding var bluetooth = Settings()
var body: some View {
return NavigationButton(destination: ToggleBluetoothView()) {
HStack() {
Image("default")
.resizable()
.cornerRadius(12)
.frame(width: 25, height: 25)
.clipped()
.aspectRatio(contentMode: .fit)
Text("Bluetooth")
.color(.blue)
.font(.system(size: 18))
Text(bluetooth.isBluetoothOn ? "On" : "Off")
.color(.gray)
.font(.subheadline)
.frame(width: 50, height: 40, alignment: .trailing)
}
}
}
}
//切换BluetoothView.swift
struct ToggleBluetoothView: View {
@ObjectBinding var bluetooth = Settings()
var body: some View {
Form {
Section(header: Text("ENABLE TO CONNECT WITH NEARBY DEVICES")) {
Toggle(isOn: $bluetooth.isBluetoothOn) {
Text("Bluetooth")
}
}
}
}
}
//Settings.swift
class Settings: BindableObject {
var didChange = PassthroughSubject<Void, Never>()
var isBluetoothOn = false { didSet { update() } }
func update() {
didChange.send(())
}
}
您在每个视图中分别实例化设置。两个视图都需要看到相同的设置对象:
更改以下内容:
NavigationButton(destination: ToggleBluetoothView(bluetooth: bluetooth)) { ... }
并删除 ToggleBluetoothView 中的初始值:
struct ToggleBluetoothView: View {
@ObjectBinding var bluetooth: Settings
var body: some View {
Form {
Section(header: Text("ENABLE TO CONNECT WITH NEARBY DEVICES")) {
Toggle(isOn: $bluetooth.isBluetoothOn) {
Text("Bluetooth")
}
}
}
}
}