Post

Replies

Boosts

Views

Activity

(Expansion) Retrieve articles from an XML parser
Please read the last thread: Call a Class into views - https://developer.apple.com/forums/thread/671482 The right code for the XML parser is almost set in stone, just I need a way to retrieve article data (not just through loadArticles()) like title, author, etc. I've decided, and was suggested, that I go the same route as my other app, Lunch Card, where there's a function where you use a dataUrl and data to call ArticlesParser(). Thing is, since the code for the XML parser is a bit more complicated, it's harder to pass variables around just like with Lunch Card. There's another class I have to deal with. So I either have to (1) redefine articles the same way with an Array; @Published var articles: [ArticleInfo] = [] ...or (2), find some other way to call articles from the ObservableObject, maybe in a StateObject or ObservedObject. I want to try and borrow as much code from Lunch Card as possible, but for an XML parser with multiple classes and extensions, it a bit easier said than done. Here's full code for ArticleInfo and ArticlesView. ArticleInfo.swift - https://developer.apple.com/forums/content/attachment/d16688a9-f420-4dee-b1f4-ed255e325a3eArticlesView - https://developer.apple.com/forums/content/attachment/fbfb99f1-87a7-448b-ad04-54aef6abe61c Not sure how ArticleInfo is an integer, so I have no idea what's going on or why it's not working as well. Also, I'm trying to experiment with borrowing the JSON decoder/encoder code, so tell me if that's the wrong way to go.
14
0
1.8k
Mar ’21
How to pass data from a TextField to a List (SwiftUI)
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.
21
0
3.8k
Dec ’20