我有兩個視圖檔案。我有文本欄位。我想從另一個視圖向文本欄位提供字串值
檔案 1 :- 創建表單的地方
struct ContentView: View {
@State var subjectLine: String = ""
var body: some View {
form {
Section(header: Text(NSLocalizedString("subjectLine", comment: ""))) {
TextField("SubjectLine", text: $subjectLine
}
}
}
}
檔案 2:- 我想為字串提供值的位置,它將顯示在文本欄位 UI 中
struct CalenderView: View {
@Binding var subjectLine: String
var body : some View {
Button(action: {
subjectLine = "Assigned default value"
}, label: {
Text("Fill textfield")
}
}
})
}
}
這是行不通的。我們可以為其他視圖檔案中的文本欄位提供值的任何其他方式。
uj5u.com熱心網友回復:
據我所知,您在 CalenderView 中有系結,這意味著您在導航更新時想要導航到那里。
struct ContentView: View {
@State private var subjectLine: String = ""
@State private var showingSheet: Bool = false
var body: some View {
NavigationView {
Form {
Section(header: Text(NSLocalizedString("subjectLine", comment: ""))) {
TextField("SubjectLine", text: $subjectLine)
}
}
.navigationBarItems(trailing: nextButton)
.sheet(isPresented: $showingSheet) {
CalenderView(subjectLine: $subjectLine)
}
}
}
var nextButton: some View {
Button("Next") {
showingSheet.toggle()
}
}
}
日歷視圖
struct CalenderView: View {
@Binding var subjectLine: String
@Environment(\.dismiss) private var dismiss
var body: some View {
Button {
subjectLine = "Assigned default value"
dismiss()
} label: {
Text("Fill textfield")
}
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/524217.html
標籤:IOS迅速苹果手机迅捷
