Posts

Post marked as solved
7 Replies
2.1k Views
This is an expansion of an earlier thread, How to pass data from a TextField to a List (SwiftUI) - https://developer.apple.com/forums/thread/669852, but I strayed away from the topic and was advised to start a new thread. To start, there are 2 views I need to work on; CardsView and AddView. CardsView: Here's the raw code: // //  CardsView.swift //  Lunch Card (iOS) // //  Created by Joshua Srery on 12/17/20. //  Additional code by OOPer on Apple Developer Forums // import SwiftUI struct Card: Identifiable {     let id = UUID()     let title: String } struct CardsView: View {     @StateObject var cardsInfo = CardsInfo()     @State var showSheetView = false     @State private var editMode = EditMode.inactive     @State private var cards: [Card] = []         private static var count = 0     var body: some View {         NavigationView {             List {                 ForEach(cards) { cards in                     NavigationLink(destination: CardFullView(cname: cardsInfo.newCard.cname, name: cardsInfo.newCard.name, id: cardsInfo.newCard.id)) {                         CardRow(cname: cardsInfo.newCard.cname, name: cardsInfo.newCard.name, id: cardsInfo.newCard.id)                     }                 }                 .onDelete(perform: onDelete)                 .onMove(perform: onMove)             }                 .navigationTitle("Cards")                 .toolbar {                     ToolbarItem(placement: .navigationBarLeading) {                         EditButton()                     }                     ToolbarItem(placement: .navigationBarTrailing) {                         Button(action: {                             self.showSheetView.toggle()                         }) {                             Image(systemName: "plus")                         }.sheet(isPresented: $showSheetView) {                             AddView(cardInfo: $cardsInfo.newCard)                     }                 }             }             .environment(\.editMode, $editMode)         }     }     private var addButton: some View {         switch editMode {         case .inactive:             return AnyView(Button(action: onAdd) { Image(systemName: "plus") })         default:             return AnyView(EmptyView())         }     }              func onAdd() {         withAnimation {             cards.append(Card(title: "Card #\(Self.count)"))                 Self.count += 1         }     }        private func onDelete(offsets: IndexSet) {         cards.remove(atOffsets: offsets)     }          private func onMove(source: IndexSet, destination: Int) {         cards.move(fromOffsets: source, toOffset: destination)     } } As you can see, there is a List being used to show the Cards you have made. Obviously, we start with none. The identifiable Card is being used, along with a declaration of cardsInfo, which is calling CardsInfo, an ObservableObject class. AddView: Here's the raw code: // //  AddView.swift //  Lunch Card (iOS) // //  Created by Joshua Srery on 12/18/20. // import SwiftUI struct AddView: View {     @Binding var cardInfo: CardInfo     var body: some View {         NavigationView {             VStack {                 CardView(name: cardInfo.name, id: cardInfo.id)                 TextField("Name", text: $cardInfo.name)                     .textFieldStyle(RoundedBorderTextFieldStyle())                     .shadow(radius: 10)                 TextField("ID", text: $cardInfo.id)                     .textFieldStyle(RoundedBorderTextFieldStyle())                     .shadow(radius: 10)                 TextField("Card Name", text: $cardInfo.cname)                     .textFieldStyle(RoundedBorderTextFieldStyle())                     .shadow(radius: 10)                 Button(action: {}) {                     Text("Create")                         .bold()                 }                 .disabled(self.cardInfo.name.isEmpty		self.cardInfo.id.isEmpty		self.cardInfo.cname.isEmpty)                 .foregroundColor(.white)                 .padding()                 .padding(.horizontal, 100)                 .background(Color.accentColor)                 .cornerRadius(10)             }.padding()             .navigationTitle(cardInfo.cname)             .navigationBarTitleDisplayMode(.inline)    	   }     } } This view contains a @Binding calling on CardInfo, which is with the CardsInfo class in CardInfo.swift: // //  CardsInfo.swift //  Lunch Card (iOS) // //  Created by Joshua Srery on 12/21/20. //  Code provided by OOPer on Apple Developer Forums // import Foundation struct CardInfo {     var name: String = ""     var id: String = ""     var cname: String = "" } class CardsInfo: ObservableObject {     @Published var newCard: CardInfo = CardInfo() } By the way, cname means Card Name. AddView contains three TextFields and a Button. The TextFields are for the user's name, card ID, and card name respectively. The button says Create, and has no action at the moment. The onAdd function in CardsView adds the information from the filled out cardsInfo to the List. I need the Create button in AddView to accomplish the same thing, but still adding it to CardView's list.
Posted
by JoshATA.
Last updated
.
Post marked as solved
21 Replies
2.6k Views
I have an app I'm working on where you make little digital ID cards with four pieces of data that I need to pass from a TextField to a List: Your Name (name) Your ID for Card (id) QR Code or Barcode? (qrcode Toggle) Card Name (cname) This is my CardsView: import SwiftUI struct Card: Identifiable {     let id = UUID()     let title: String } struct CardsView: View {     @State private var editMode = EditMode.inactive     @State private var cards: [Card] = []         private static var count = 0     var body: some View {         NavigationView {             List {                 ForEach(cards) { cards in                     NavigationLink(destination: CardFullView(cname: "Moore Lunch")) {                         CardRow(cname: "Lunch", name: "John Doe", id: "123456")                     }                 }                 .onDelete(perform: onDelete)                 .onMove(perform: onMove)             }                 .navigationTitle("Cards")                 .toolbar {                     ToolbarItem(placement: .navigationBarLeading) {                         EditButton()                     }                     ToolbarItem(placement: .navigationBarTrailing) {                         NavigationLink(                             destination: AddView(),                             label: {                                 Image(systemName: "plus")                             })                     }             }             .environment(\.editMode, $editMode)         }     }     private var addButton: some View {         switch editMode {         case .inactive:             return AnyView(Button(action: onAdd) { Image(systemName: "plus") })         default:             return AnyView(EmptyView())         }     }     func onAdd() {         cards.append(Card(title: "Card #\(Self.count)"))             Self.count += 1     }     private func onDelete(offsets: IndexSet) {         cards.remove(atOffsets: offsets)     }     private func onMove(source: IndexSet, destination: Int) {         cards.move(fromOffsets: source, toOffset: destination)     } } struct CardsView_Previews: PreviewProvider {     static var previews: some View {         CardsView()     } } ...and my AddView: import SwiftUI struct CardFullView: View {     let cname: String     var body: some View {         NavigationView {             CardView(name: "John Doe", id: "123456")                 .navigationTitle(cname)                 .navigationBarTitleDisplayMode(.inline)         }     } } struct CardFullView_Previews: PreviewProvider {     static var previews: some View {         CardFullView(cname: "Lunch")     } } CardRow shows an HStack, consisting of a generated QR Code or Barcode, a VStack with a title showing cname, a headline showing name, and a subheadline showing id. I want to pass the value of the TextFields into the CardRows.
Posted
by JoshATA.
Last updated
.