重置按钮上的视图单击 SwiftUI
Reset View on Button Click SwiftUI
我想在点击“清除”或“重置”等按钮时重置视图。这是我目前的进度:
struct Cell: Hashable {
let value: Int
}
struct CellView: View {
var cell: Cell
@State private var isDisabled = false
var body: some View {
Button {
isDisabled = true
} label: {
Text("\(cell.value)")
.frame(width: 100, height: 100)
}.disabled(isDisabled)
.border(Color.black)
}
func toggleDisabled() {
isDisabled = false
}
}
用户只能点击按钮一次,除非他们重置面板。最初的想法是跟踪 CellViews,这样我就可以像这样使用 toggleDisabled() 切换状态
struct ContentView: View {
var cellViews = [
CellView(cell: Cell(value: 1)),
CellView(cell: Cell(value: 2))
]
var body: some View {
cellViews[0]
cellViews[1]
Button("Clear") {
cellViews.forEach{ [=11=].toggleDisabled() }
}
}
}
但我 运行 很难做到这一点。
我考虑过将 isDisabled 存储在 Cell 中并使 CellView 对其具有绑定,但这也是不可能的。实现这一目标的正确方法是什么?
您可以使用 ObservableObject
模型尝试这种方法来跟踪您的 Cell 状态。例如:
class CellViewModel: ObservableObject {
@Published var cells = [Cell]()
func resetAll() {
for i in cells.indices {
cells[i].isDisabled = false
}
}
}
struct CellView: View {
@Binding var cell: Cell
var body: some View {
Button {
cell.isDisabled = true
} label: {
Text("\(cell.value)").frame(width: 100, height: 100)
}.disabled(cell.isDisabled)
.border(Color.black)
}
}
struct ContentView: View {
@StateObject var cellModel = CellViewModel()
var body: some View {
VStack {
List {
ForEach($cellModel.cells) { $cell in
CellView(cell: $cell)
}
}
Button("Reset all") {
cellModel.resetAll()
}
}
.onAppear {
cellModel.cells.append(Cell(value: 1))
cellModel.cells.append(Cell(value: 2))
}
}
}
struct Cell: Identifiable, Hashable {
let id = UUID()
let value: Int
var isDisabled = false
}
您认为是正确的。为此,使单元格@State。将 isDisabled 移动到 Cell 中。将单元格的绑定传递到 CellView。
我想在点击“清除”或“重置”等按钮时重置视图。这是我目前的进度:
struct Cell: Hashable {
let value: Int
}
struct CellView: View {
var cell: Cell
@State private var isDisabled = false
var body: some View {
Button {
isDisabled = true
} label: {
Text("\(cell.value)")
.frame(width: 100, height: 100)
}.disabled(isDisabled)
.border(Color.black)
}
func toggleDisabled() {
isDisabled = false
}
}
用户只能点击按钮一次,除非他们重置面板。最初的想法是跟踪 CellViews,这样我就可以像这样使用 toggleDisabled() 切换状态
struct ContentView: View {
var cellViews = [
CellView(cell: Cell(value: 1)),
CellView(cell: Cell(value: 2))
]
var body: some View {
cellViews[0]
cellViews[1]
Button("Clear") {
cellViews.forEach{ [=11=].toggleDisabled() }
}
}
}
但我 运行 很难做到这一点。
我考虑过将 isDisabled 存储在 Cell 中并使 CellView 对其具有绑定,但这也是不可能的。实现这一目标的正确方法是什么?
您可以使用 ObservableObject
模型尝试这种方法来跟踪您的 Cell 状态。例如:
class CellViewModel: ObservableObject {
@Published var cells = [Cell]()
func resetAll() {
for i in cells.indices {
cells[i].isDisabled = false
}
}
}
struct CellView: View {
@Binding var cell: Cell
var body: some View {
Button {
cell.isDisabled = true
} label: {
Text("\(cell.value)").frame(width: 100, height: 100)
}.disabled(cell.isDisabled)
.border(Color.black)
}
}
struct ContentView: View {
@StateObject var cellModel = CellViewModel()
var body: some View {
VStack {
List {
ForEach($cellModel.cells) { $cell in
CellView(cell: $cell)
}
}
Button("Reset all") {
cellModel.resetAll()
}
}
.onAppear {
cellModel.cells.append(Cell(value: 1))
cellModel.cells.append(Cell(value: 2))
}
}
}
struct Cell: Identifiable, Hashable {
let id = UUID()
let value: Int
var isDisabled = false
}
您认为是正确的。为此,使单元格@State。将 isDisabled 移动到 Cell 中。将单元格的绑定传递到 CellView。