I made you a minimal example with NavigationView
and @ObservedObjects
in each View
. This shows the basic usage of ObservableObject
with nested class
. This works because each View
gets passed the Model
and "observes" it.
If you have any questions please read the documentation first before asking them. You should find most of the stuff under Combine
and SwiftUI
.
Please Note int64
does not exist as far as I know and your Array
declarations are wrong too! I corrected them in the example I provided.
class PageModel: ObservableObject {
@Published var id: Int
@Published var content: String
init(id: Int, content: String) {
self.id = id
self.content = content
}
}
class BookModel: ObservableObject {
@Published var id: Int
@Published var title: String
@Published var pages: [PageModel] = []
init(id: Int, title: String) {
self.id = id
self.title = title
}
func addDummies() {
DispatchQueue.main.async {
self.pages.append(PageModel(id: 0, content: "To"))
self.pages.append(PageModel(id: 1, content: "tell"))
self.pages.append(PageModel(id: 2, content: "you"))
self.pages.append(PageModel(id: 3, content: "I'm"))
self.pages.append(PageModel(id: 4, content: "sorry..."))
self.pages.append(PageModel(id: 5, content: "for"))
self.pages.append(PageModel(id: 6, content: "everything"))
self.pages.append(PageModel(id: 7, content: "that"))
self.pages.append(PageModel(id: 8, content: "I've"))
self.pages.append(PageModel(id: 9, content: "done..."))
}
}
}
class ShelfModel: ObservableObject {
@Published var id: Int
@Published var title: String
@Published var books: [BookModel] = []
init(id: Int, title: String) {
self.id = id
self.title = title
}
func add() {
DispatchQueue.main.async {
self.books.append(BookModel(id: self.books.count, title: "frick I am new"))
}
}
func addDummies() {
DispatchQueue.main.async {
self.books.append(BookModel(id: 0, title: "Hello"))
self.books.append(BookModel(id: 1, title: "from"))
self.books.append(BookModel(id: 2, title: "the"))
self.books.append(BookModel(id: 3, title: "other"))
self.books.append(BookModel(id: 4, title: "side..."))
self.books.append(BookModel(id: 5, title: "I"))
self.books.append(BookModel(id: 6, title: "must"))
self.books.append(BookModel(id: 7, title: "have"))
self.books.append(BookModel(id: 8, title: "called"))
self.books.append(BookModel(id: 9, title: "a thousand"))
self.books.append(BookModel(id: 10, title: "times..."))
}
}
}
struct PageView: View {
@ObservedObject var page: PageModel
var body: some View {
HStack {
Text("\(page.id)")
Text("\(page.content)")
}
}
}
struct BookView: View {
@ObservedObject var book: BookModel
var body: some View {
VStack {
HStack {
Text("\(book.id)")
Text("\(book.title)")
}
List(book.pages, id: \.id) { page in
PageView(page: page)
}
}
.navigationBarItems(trailing: Button("Add Page") {
self.book.addDummies()
})
}
}
struct ContentView: View {
@ObservedObject var shelf = ShelfModel(id: 0, title: "Lolz")
var body: some View {
VStack {
NavigationView {
List(self.shelf.books, id: \.id) { book in
NavigationLink(destination: BookView(book: book)) {
Text("\(book.title)")
}.navigationBarItems(trailing: Button("Add Book") {
self.shelf.add()
})
}
}
}.onAppear {
self.shelf.addDummies()
}
}
}
Tested on iPad Pro.
I hope this helps!