I want to make a button to go on other ViewFile but when I try, The button or text don't go to my other file, how I can solve it ?
I have try
Button("Accéder à l'actualité"){
NavigationLink(destination: HomeView(), label: {
EmptyView()
})
}
.buttonStyle(.bordered)
.tint(Color.blue)
.foregroundColor(Color.black)
But I have error
I want to connect : img1 to img2 img1 to img2
I want when I click on The button "Accéder à l'actualité" I switch on The other View/file
This is my code : StartView file :
import SwiftUI
struct StartView: View {
@State private var willMoveToNextScreen = false
var body: some View {
VStack {
Image(systemName: "newspaper")
.padding(.bottom, 2.0)
.imageScale(.large)
.foregroundColor(.accentColor)
Text("NEWS APP")
.font(.title)
.fontWeight(.ultraLight)
.foregroundColor(Color.black)
.multilineTextAlignment(.center)
Button("Accéder à l'actualité"){
NavigationLink(destination: HomeView(), label: {
EmptyView()
})
}
.buttonStyle(.bordered)
.tint(Color.blue)
.foregroundColor(Color.black)
}
.padding()
}
}
struct StartView_Previews: PreviewProvider {
static var previews: some View {
StartView()
}
}
and HomeView file
import SwiftUI
struct HomeView: View {
var body: some View {
NavigationView {
List(AllNews.list, id: \.id){
AllNews in ArticleItemView(AllNews: AllNews)
.listRowSeparator(.hidden)
.overlay(
NavigationLink(
destination: AllNewsDescriptionView(AllNews: AllNews), label: {
EmptyView()
}
).opacity(0)
)
}.listStyle(PlainListStyle())
.navigationTitle("Articles")
}
}
}
struct ArticleItemView: View {
let AllNews: AllNews
var body: some View {
ZStack{
Color.white
.cornerRadius(8)
HStack{
image
info
}.padding()
}.shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: 2)
}
}
private extension ArticleItemView {
var image : some View {
Image(AllNews.image)
.resizable()
.scaledToFit()
.frame(width: 70, height: 50)
}
var info : some View {
VStack (alignment: .leading){
Text(AllNews.name)
.font(.title3)
.fontWeight(.semibold)
.lineLimit(1)
Text(AllNews.description)
.font(.footnote)
.foregroundColor(Color.gray)
.lineLimit(2)
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
How I can solve it ?
NavigationLink
only works inside a NavigationStack
or the deprecated NavigationView
.
It is like a button, so in StartView
use it like this without the Button
:
NavigationStack {
// ...
NavigationLink("Accéder à l'actualité", destination: HomeView())
// ...
}
There is no need for another NavigationView
in HomeView
.
EDIT-1:
Here is my test code, to show that my answer works:
struct ContentView: View {
var body: some View{
StartView()
}
}
struct StartView: View {
@State private var willMoveToNextScreen = false
var body: some View {
NavigationStack { // <--- here
VStack {
Image(systemName: "newspaper")
.padding(.bottom, 2.0)
.imageScale(.large)
.foregroundColor(.accentColor)
Text("NEWS APP")
.font(.title)
.fontWeight(.ultraLight)
.foregroundColor(Color.black)
.multilineTextAlignment(.center)
// --- here, NO Button
NavigationLink("Accéder à l'actualité", destination: HomeView())
.buttonStyle(.bordered)
.tint(Color.blue)
.foregroundColor(Color.black)
}
.padding()
}
}
}
struct HomeView: View {
// for testing
@State var allnews = ["news-1","news-2","news-3"]
var body: some View {
// ---> NO need for NavigationView here
List(allnews, id: \.self){ news in
Text(news)
.listRowSeparator(.hidden)
.overlay(
// for testing
NavigationLink("", destination: Text("destination \(news)")).opacity(0)
)
}.listStyle(PlainListStyle())
.navigationTitle("Articles")
}
}