How can I dismiss a sheet and have it load a new view once a sheet is dismissed?

I am creating a game and at the end, want the sheet to appear to end the game. Eventually, it will have a few more options, but I need to get this working first. This is the first app I am creating, so I’m not sure whether this is the best way to handle it.

I added this code, and it has been working for the most part. However, I found that nothing changes when I hit New Game. If I remove the sheet dismissal, I find that the view is loading on the sheet that is being dismissed. What can I change to have it so that once a sheet is dismissed, it will load the new view instead of the current game view?

import SwiftUI

struct GameEndCustomSheet: View {
    @Environment(.presentationMode) var presentationMode
    @State private var navigateToNewGame = false
    
    var body: some View {
        NavigationStack {
            VStack {
                Text("Game Ended")
                
                Button(action: {
                    // Dismiss the sheet
                    presentationMode.wrappedValue.dismiss()
                }) {
                    Text("New Game")
                }
            }
            .onDisappear {
                // Navigate to the new view when the sheet disappears
                navigateToNewGame = true
            }
            .navigationDestination(isPresented: $navigateToNewGame) {
                ContentView()
            }
        }
    }
}

struct GameEndCustomSheet_Previews: PreviewProvider {
    static var previews: some View {
        GameEndCustomSheet()
    }
}


New contributor

ObliviousOre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Here are a few thoughts:

  1. Note the difference between pushing a view onto the navigation stack, and modally presenting a view. From your code, my understanding is that you want to modally presenting a sheet when the game is over, and when the New Game button is tapped, a new GameView is pushed onto the nav stack. Below is the code based on that assumption.
import SwiftUI

struct ContentView: View {
    @State private var gameEndPageIsPresented = false
    @State private var navigateToNewGame = false
    
    var body: some View {
        NavigationStack {
            Button("Show Game Page") {
                gameEndPageIsPresented = true
            }
            .sheet(isPresented: $gameEndPageIsPresented) {
                GameEndCustomSheet()
                    .onDisappear {
                        // Navigate to the new view when the sheet disappears
                        navigateToNewGame = true
                    }
            }
            .navigationDestination(isPresented: $navigateToNewGame) {
                GameView()
            }
        }
    }
}

struct GameEndCustomSheet: View {
    @Environment(.dismiss) private var dismiss
    
    var body: some View {
        VStack {
            Text("Game Ended")
            Button("New Game") {
                // Dismiss the sheet
                dismiss()
            }
        }
    }
}

struct GameView: View {
    var body: some View {
        Text("This is another view")
    }
}
  1. If you want to have a navigation stack for the modally presented sheet, then the GameEndCustomSheet cannot be the root view of the nav stack. Because once it is dismissed, the NavigationStack is destroyed, and all its modifiers will be invalidated as well.

  2. Say you want to have multiple views as the root view of a navigation stack, you can create a enum as a state machine, and pass that enum value to the various views you want to display (via a view model, environment value/object, bindings, constant, etc.) So when the value is changed, the root view of a nav stack can redraw based on a switch-case conditional view.

  3. If your navigation structure is more complex than a tree-like structure, consider adopting the “Coordinator” pattern in your app (search SwiftUI + Coordinator Pattern). This will allow you to navigate between views via “routes”, instead of push-popping on branches of a tree.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật