回到开始屏幕会导致视图相互堆叠

Going back to Start Screen leads into stacking the views in each other

每次我回到开始屏幕时,它似乎都会自行堆叠。你可以在我附上的截图中看到我的意思。我添加了一些边框所以你可以明白我的意思

开始屏幕的代码:

struct StartScreen: View {

    
    var body: some View {
        NavigationView{
            
            
            VStack() {
                
                Image("Headline").resizable().scaledToFit()
                Image("GreenMonster")
                    .resizable()
                    .scaledToFit()
                    .frame(alignment: .top)
 
                NavigationLink(destination: Game(monster: monster)) {
                    Text("Spielen")
                        .frame(width: 200, height: 50, alignment: .center)
                        .font(.title)
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(40)
                        .foregroundColor(.white)
                        .padding(10)
                        .overlay(
                            RoundedRectangle(cornerRadius: 40)
                                .stroke(Color.blue, lineWidth: 5)
                    )
                    
                }.isDetailLink(false)
                
                /*
                 NavigationLink(destination: Settings()){
                 Image("Settingswheel").resizable().scaledToFit().frame(width: 50, height: 50).offset(x: 150)
                 }
                 
                 */
                
            }
            
            }.navigationBarBackButtonHidden(true).border(Color.green)
            
    }
}

返回的代码是:

struct DefeatedView: View {
    
    @EnvironmentObject var helper: Helper
    
    var body: some View {
        
        NavigationView(){
            VStack(){
                Text("BESIEGT!").foregroundColor(.green).font(.title).bold()
                Image(monster[0].imageURL).resizable().scaledToFit()
                NavigationLink(destination: StartScreen()){
                    Text("Zum Start").frame(width: 120, height: 6, alignment: .center)
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(40)
                        .foregroundColor(.white)
                        .padding(10)
                        .overlay(
                            RoundedRectangle(cornerRadius: 40)
                                .stroke(Color.blue, lineWidth: 5)
                    )
                }
            }
        }.navigationBarBackButtonHidden(true)
        
    }
}

感谢您的帮助,我是 SwiftUI 的新手

将此添加到您的 DefeatedView

@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

然后不再使用 NavigationLink,而是使用一个按钮并将您的视图手动推回到开始视图

Button(action: {
    //Push navigation view back
    self.presentationMode.wrappedValue.dismiss()
})
{
    Text("Zum Start").frame(width: 120, height: 6, alignment: .center)
        .padding()
        .background(Color.blue)
        .cornerRadius(40)
        .foregroundColor(.white)
        .padding(10)
        .overlay(
            RoundedRectangle(cornerRadius: 40)
                .stroke(Color.blue, lineWidth: 5)
    )
}

编辑:

当您按 NavigationView 两次时,只调用一次演示模式确实会推回您的游戏视图。这是 ObservableObject.

的可能解决方案
class ViewHelper : ObservableObject
{
    @Published var finishedGame : Bool = false
}

struct StartScreen: View {

    @EnvironmentObject var viewHelper : ViewHelper

    var body: some View {
    
    NavigationLink(destination: Game(), isActive: self.$viewHelper.finishedGame) {
        Text("Spielen")

然后当游戏结束时,更改 finishedGame 变量。

struct DefeatedView: View {
        
    @EnvironmentObject var viewHelper : ViewHelper

    var body: some View {
        
        NavigationView(){
            VStack(){
                Text("BESIEGT!").foregroundColor(.green).font(.title).bold()
                Button(action: {
                    self.viewHelper.finishedGame = false
                })
                {
                    Text("Zum Start").frame(width: 120, height: 6, alignment: .center)
                        .padding()
                        .background(Color.blue)
                        .cornerRadius(40)
                        .foregroundColor(.white)
                        .padding(10)
                        .overlay(
                            RoundedRectangle(cornerRadius: 40)
                                .stroke(Color.blue, lineWidth: 5)
                    )
                }
            }
        }.navigationBarBackButtonHidden(true)

          

我在这个网站上找到了我的问题的解决方案:

https://thinkdiff.net/ios/swiftui-how-to-pop-to-root-view/