2

I am trying out to implement a Mailview like I've seen in a tutorial before. After solving the layout, I tried to get some features on it.

If I begin to write a new message, I want to clear the filled Textfield boxes, when the user clicks the trash bin. But however I don't find out how to clear it.

import SwiftUI var inboundMails = [[String()]] var outboundMails = [[String()]] struct ContentView: View { @State var showComposeMessageView: Bool = false var body: some View { TabView { NavigationView { List (0 ..< 6) { _ in NavigationLink(destination: Text("Nachrichtentext")) { SingleMessageView() } } .listStyle(GroupedListStyle()) .navigationTitle("Inbox") .navigationBarItems(trailing: Button(action: { showComposeMessageView.toggle() }, label: { Image(systemName: "square.and.pencil") }) ) } // Verfassen Button .sheet(isPresented: $showComposeMessageView, content: { NewMessage() }) //.sheet .tabItem { Image(systemName: "envelope.fill") Text("Inbox") } Text("Sent") .tabItem { Text("Sent") Image(systemName: "paperplane.fill") } } } } // Design eines einzelnen Nachrichtenblocks struct SingleMessageView: View { var body: some View { HStack { Image(systemName: "person.circle.fill") .resizable() .frame(width: 40, height: 40, alignment: .center) .foregroundColor(.gray) VStack(alignment: .leading) { HStack { Text("Absender") .font(.headline) Spacer() Text("01-07-2020") .font(.subheadline.monospacedDigit()) .foregroundColor(.secondary) } Text("Betreff") .font(.subheadline) .lineLimit(2) } } } } //Neue Nachricht verfassen struct NewMessage: View { @State var messageText = "" @State var betreff = "" @State var cc = "" @State var empfaenger = "" @State var sendButton: Bool = false var body: some View { ZStack { Color.white Spacer() VStack(alignment: .leading) { // Kopfzeile HStack(alignment: .top) { Spacer() Image(systemName: "paperplane.circle") .resizable() .frame(width: 70, height: 70, alignment: .leading) VStack(alignment: .leading) { Text("Empfänger: ") Spacer() Text("Betreff: ") Spacer() Text("CC:") }.frame(height: 70) VStack { TextField("Empfänger", text: $empfaenger).lineLimit(2) .font(.subheadline) Spacer() TextField("Betreff", text: $betreff).lineLimit(2) .font(.subheadline) Spacer() TextField("CC", text: $cc) .font(.subheadline) }.frame(height: 70) } .foregroundColor(.black) .background(Color.white) .font(.headline) // Knöpfe HStack { Spacer() // Senden Button( action: { sendButton.toggle() outboundMails = Mails().MailManager(inbound: false, from_to: empfaenger, cc: cc, subject: betreff, message: messageText, mailarray: outboundMails)}, label: {Image(systemName: "paperplane")}) .frame(width: 20, height: 20, alignment: .leading) Spacer() // Farbe bearbeiten Button( action: { sendButton.toggle() }, label: {Image(systemName: "pencil")}) .frame(width: 20, height: 20, alignment: .leading) Spacer() // Anlage hinzufügen Button( action: { sendButton.toggle() }, label: {Image(systemName: "doc")}) .frame(width: 20, height: 20, alignment: .leading) Spacer() // Verwerfen Button( action: { sendButton.toggle() messageText = "" cc = "" betreff = "" empfaenger = "" }, label: {Image(systemName: "trash")}) .frame(width: 20, height: 20, alignment: .leading) Spacer() }.frame(height: 30, alignment: .top) // NachichtentextFeld TextEditor(text: $messageText) .frame(width: 411, height: 400, alignment: .top) .border(Color.gray) Spacer() } } } } // Design eines einzelnen Nachrichtenblocks struct SendMessageView: View { var body: some View { HStack { Image(systemName: "person.circle.fill") .resizable() .frame(width: 40, height: 40, alignment: .center) .foregroundColor(.gray) VStack(alignment: .leading) { HStack { Text("Absender") .font(.headline) Spacer() Text("01-07-2020") .font(.subheadline.monospacedDigit()) .foregroundColor(.secondary) } Text("Betreff") .font(.subheadline) .lineLimit(2) } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } ``` Thanks in advance for your help! 
10
  • hi, interesting, perhaps this might be of interest stackoverflow.com/questions/320078/… Commented Jul 5, 2021 at 22:38
  • 3
    Simply set the text property for that text field to be an empty string. Commented Jul 5, 2021 at 22:41
  • 1
    The code does that. In fact, it looks fine as is and it works for me in a the SwiftUI preview. Commented Jul 5, 2021 at 23:53
  • @Seaspell Can you explain this a Bit More? I Ding know Home the set the value of this Field to nil or empty String. I only can change the stored variables but it doesnt clear the Texfields on view Commented Jul 7, 2021 at 0:10
  • @Eric Shieh of I press the bin Button, Alls Fields should Clearstream. It dont do this in my Simulation :/ Commented Jul 7, 2021 at 0:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.