I have this two views:
import SwiftUI
import QGrid
struct CategoriasCanales: View {
@Binding var logueado:Bool
@ObservedObject var categorias = CargarCategoriasCanales()
var body: some View {
NavigationView{
QGrid(self.categorias.listaCategoriasCanales, columns: 3){item in
NavigationLink(destination:VerCanales(categoria: item.categoryID)){
Text(item.categoryName).frame(width:400, height:50)
}
}
}
}
}
and
import SwiftUI
import QGrid
struct VerCanales: View {
private var categoria: String
@ObservedObject private var canales: CargarCanales
init(categoria: String) {
print(categoria)
self.categoria = categoria
self.canales = CargarCanales(categoriaID: categoria)
}
var body: some View {
NavigationView{
QGrid(self.canales.listaCanales, columns: 3){item in
NavigationLink(destination:PlayerVersa(canalID: item.streamID)){
Text(item.name).frame(width:400, height:50)
}
}
}
}
}
The problem is that when I enter the "CategoriasCanales" view, it runs the init of the "VerCanales" view a number of times equal to the number of categories.
I am new to SwiftUI, in DART for example, the Print() in the ChannelView init would only print the category that I am sending as a parameter, not all categories.
The NavigationLink
creates destination view in place of initialization, so you see called VerCanales.init
as many as self.categorias.listaCategoriasCanales
count.
I assume you wanted to defer creation of VerCanales
: (used DeferView
from https://stackoverflow.com/a/61242931/12299030)
NavigationLink(destination:DeferView { VerCanales(categoria: item.categoryID) }){
Text(item.categoryName).frame(width:400, height:50)
}
Note: you don't need second NavigationView
in VerCanales
, only one navigation view should be in navigation stack and there is that one already in CategoriasCanales