請幫我解決這個問題:
重現我的問題的步驟:
點擊“編輯我的名字”按鈕
在 .sheet 中,點擊 TextField,然后在仍然顯示鍵盤的情況下,一直向下滾動
點擊“洗掉姓名”按鈕
這是問題所在:
確認對話框只出現一秒鐘,然后消失,沒有給用戶任何機會(或少于一秒鐘的機會)點擊確認對話框的按鈕之一!
這是我的代碼:
內容視圖.swift
import SwiftUI
struct ContentView: View {
@State private var myName = "Joe"
@State private var isEditingName = false
var body: some View {
Text("My name is: \(myName)")
Button("Edit My Name") {
isEditingName = true
}
.padding()
.sheet(isPresented: $isEditingName) {
EditView(name: $myName)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
編輯視圖.swift
import SwiftUI
struct EditView: View {
@Binding var name: String
@State private var isShowingConfirmationDialog = false
var body: some View {
Form {
Section {
TextField("Name", text: $name)
}
Section {
VStack {
ForEach(0..<50, id: \.self) { number in
Text("\(number)")
}
}
}
Section {
deleteNameWithConfirmationDialog
}
}
}
private var deleteNameWithConfirmationDialog: some View {
Button("Delete Name", role: .destructive) {
isShowingConfirmationDialog = true
}
.confirmationDialog("Are you sure you want to delete name?", isPresented: $isShowingConfirmationDialog) {
Button("Delete Name", role: .destructive) {
name = ""
}
Button("Cancel", role: .cancel) { }
} message: {
Text("Are you sure you want to delte name?")
}
}
}
struct EditView_Previews: PreviewProvider {
static var previews: some View {
EditView(name: .constant(String("Joe")))
}
}
uj5u.com熱心網友回復:
如果您將其.confirmationDialogue移出Form:
struct EditView: View {
@Binding var name: String
@State private var isShowingConfirmationDialog = false
var body: some View {
Form {
Section {
TextField("Name", text: $name)
}
Section {
VStack {
ForEach(0..<50, id: \.self) { number in
Text("\(number)")
}
}
}
Section {
Button("Delete Name", role: .destructive) {
isShowingConfirmationDialog = true
}
}
}
.confirmationDialog("Are you sure you want to delete name?", isPresented: $isShowingConfirmationDialog) {
Button("Delete Name", role: .destructive) {
name = ""
}
Button("Cancel", role: .cancel) { }
} message: {
Text("Are you sure you want to delete name?")
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442051.html
