数日前から、テキストの複製の実装をしているのですが、文字を打ったテキストエディターに複製のfuncをかけても、ただ文字欄が空になるだけです。
どうしたら複製できるでしょうか?
swift
12struct ContentView: View { 3 4@Environment(\.managedObjectContext) private var viewContext 5@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], animation: .default) private var items: FetchedResults<Item>6 7var body: some View {8 9NavigationView {10 11List{12Section(header: Text("Simple Memo")){13ForEach(items){Item in 14Text(item.text ?? "no text")15}16}17 18.toolbar{19 20 21ToolbarItemGroup(placement: .bottomBar) {22 23NavigationLink(destination: PostView(), label: {Image(systemName: "square.and.pencil")})24 25}26 27 28}29 30}31 32 33}34 35}36 37 38struct PostView: View {39 @Environment(\.managedObjectContext) private var viewContext 40 @FetchRequest(41 sortDescriptors: [NSSortDescriptor(keyPath: \Item.makeDate, ascending: true)],42 animation: .default)43 private var item: FetchedResults<Item>44 @State var textEditorText = ""45 46 var body: some View {47 TextEditor(text: $textEditorText)48 .font(.body)49 .textInputAutocapitalization(.never)50 .onDisappear(perform: additem)51 .frame(width: .infinity, height: .infinity)52 .toolbar { 53 54ToolbarItemGroup(placement: .navigationBarTrailing) {55 56Button(action: {duplication()}) {Image(systemName: "circle")}57}58}59 }60 61 func additem() {62 withAnimation {63 let newItem = Item(context: viewContext)64 newItem.text = textEditorText 65 try? viewContext.save()66 }67 }68 69func duplication() {70 let sub = textEditorText 71 additem()72 textEditorText = sub 73 }74 75}76

0 コメント