swiftui - Edit Text Editor SwiftUI
问题描述
I am trying to edit the text of a binding value from another view. But text editor does not allow me to do so.
First I need to see the previous value of selectedNote.title in TextEditor and then I should be able to edit the title.
struct EditNotePageView: View {
@Binding var selectedNote: CDNoteModel!
@State var text = ""
var body: some View {
TextEditor(selectedNote.title!, text: $text)
}
}
I also tried like this. I can see my selectedNote.title in the simulator but when I tap on it it does nothing.
struct EditNotePageView: View {
@Binding var selectedNote: CDNoteModel!
@State var text = ""
var body: some View {
Text(selectedNote.title!)
.onTapGesture {
text = selectedNote.title!
TextEditor(text: $text)
}
}
}
解决方案
ZStack Text 和 textEditor 用一点点 .onAppear 解决了这个问题
struct EditNotePageView: View {
@Binding var selectedNote: CDNoteModel!
@State var text = ""
var body: some View {
ZStack(alignment: .leading){
Text(selectedNote.title!)
.onAppear {
text = selectedNote.title!
}
TextEditor(text: $text)
}
}
}
推荐阅读
- android - Map isn't neither zoomable nor movable after drawer navigation
- javascript - setting a variable in javascript to be used in another form
- delphi - 如何用车牌格式化TEdit(Delphi)
- delegates - How I can use InvoiceShipment Delegate PXOverride in ShipmentEntry acumatica
- powershell - 如何使用 Graph API 或 Powershell 在 Azure AD 中使用 SAML SSO 创建新的企业应用程序
- reactjs - React native run-android 卡在读取文件权限上
- date - 如何在 where 子句中引用另一个 CTE 的日期范围而不加入它?
- tensorflow - tf.trainable_variables() 返回一个空白列表
- python - Biopython 是否有“排除未培养的样本”和“类型材料中的序列”的论据?
- python - 如何根据行的值 Pandas 数据框查找两个不同的范围