Hi,
I have an app which creates a grid layout with the help of LazyVGrid.
Each grid item has a navigation link to the target view with parameters for the target view.
Inside the grid item exists a menu button. When the user click on the menu button, the action should get the grid item valus and pass it to the same target view with the same paramters but with other values for the parameters.
Is that possible? I tried it several hours without success.
I tried the code below and get the following error message.
SwiftUI encountered an issue when pushing aNavigationLink. Please file a bug.
ContenView
import SwiftUI
struct GameData {
var id: Int
var name: String
}
struct ContentView: View {
@State private var games = [GameData]()
@State private var showSameTargetView = false
@State private var modeValue = false
let taskHSpace: CGFloat = -30
let taskVSpace: CGFloat = 20
var body: some View {
NavigationView() {
let columns = Array(
repeating: GridItem(. flexible(), spacing: taskHSpace), count: 2)
LazyVGrid(columns: columns, spacing: taskVSpace) {
ForEach(self.games, id: \.id) {game in
NavigationLink(
destination: TargetView(id: game.id, gameName: game.name, mode : false)
) {
HStack {
Image(systemName: "person")
.resizable()
.frame(width: 75, height: 100)
.padding(15)
VStack(alignment: .leading, spacing: 10) {
Text("\(game.name)")
.font(.custom("Chalkduster", fixedSize: 18))
.foregroundColor(Color(UIColor.brown))
.padding(.top)
Spacer()
}
Spacer()
VStack(alignment: .leading) {
Menu(content: {
Button(action: {
self.modeValue = true
self.showSameTargetView = true
}) {
Text("Show Target View")
Image(systemName: "square.and.pencil")
}
}) {
Image(systemName: "ellipsis.circle")
.font(.title2)
.foregroundColor(.gray)
.padding(20)
}
.id(UUID()) // temp. bug fix - https://developer.apple.com/forums/thread/692338
.onTapGesture {} // override - avoid NavigationLink action
NavigationLink(destination: TargetView(id: game.id, gameName: game.name, mode : self.modeValue), isActive: self.$showSameTargetView)
{ EmptyView() }
Spacer()
}
}
.frame(width: 300, height: 140, alignment: .topLeading)
.background(Color.yellow)
}
}
}
}
.onAppear {
if self.games.count == 0 {
self.games.append(
GameData.init(id: 1, name: "Test1")
)
self.games.append(
GameData.init(id: 2, name: "Test2")
)
self.games.append(
GameData.init(id: 3, name: "Test3")
)
}
}
.navigationViewStyle(StackNavigationViewStyle()) // remove default split view
.navigationBarBackButtonHidden(true)
.accentColor(.black)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Navigation is possible but is it the right approach to reach my goal?
Many thanks in advance. Sven