Lontronix
Lontronix

Reputation: 193

App Crashes when attempting to present sheet over view when using SwiftUI

I am trying to present a view over another view in SwiftUI using .sheet(). When this runs my app crashes with no stack trace even when run on a simulator and this error displayed:

    EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I have already determined that the view that is being presented is not the problem by attempting to present Text("Test") instead of my custom view but I still get the same error.

.sheet(isPresented: self.$isPresenting) { () -> Text in
    return Text("This is a test")
 }

This is the code that sets up the view and attempts to present another view when the green button is pressed. It looks like this on the device:

enter image description here

struct WeekView: View {

    @ObjectBinding var currentMealPlan = MealPlan(totalMealExchanges: 0, totalGraciesMeals: 0)
    @State var isPresenting = false
    var body: some View {

        NavigationView{
        VStack{


            VStack(alignment: .leading){
                Text("Meals at Gracies remaining: ?/\(currentMealPlan.totalGraciesMeals)")
                Text("Meal Exchanges Remaining: ?/\(currentMealPlan.totalMealExchanges)")
            }



            List{
                Section(header: Text("Monday")) {
                    HStack{


                        MealView(meal: "Breakfast", location: "Dorm ", color: Color.green)
                            .tapAction {
                                self.isPresenting.toggle()

                        }
                        .sheet(isPresented: self.$isPresenting) { () -> Text in
                                    return Text("This is a test")
                                }



                        MealView(meal: "Lunch", location: "Gracies", color: Color.blue)
                        MealView(meal: "Dinner", location: "Salsarita", color: Color.yellow)



                    }
                }


  Section(header: Text("Tuesday")) {
                    Text("Hello World")

                }

                Section(header: Text("Wednesday")) {
                    Text("Hello World")

                }

                Section(header: Text("Thursday")) {
                    Text("Hello World")

                }
                Section(header: Text("Friday")) {
                    Text("Hello World")

                }

                Section(header: Text("Saturday")) {
                    Text("Hello World")

                }

                Section(header: Text("Sunday")) {
                    Text("Hello World")

                }


            }

 }.navigationBarTitle("This Week")
            .navigationBarItems(trailing: Button(action: {

            }, label: {
                Text("Help")
            }))

    }

    }
}





struct MealView: View {
    @State var meal: String
    @State var location: String
    @State var color: Color
    @State private var showPopover = false

    var body: some View {
        ZStack{
            Rectangle()
                .fill(color)
            VStack{
                Text(meal)
                    .bold()
                Text(location)
                    .lineLimit(2)
                    }


        }
    }}
class MealPlan: BindableObject   {

    let willChange =  PassthroughSubject<Void, Never>

    var totalMealExchanges: Int {didSet {willChange.send()}}
    var totalGraciesMeals: Int {didSet {willChange.send()}}

    init(totalMealExchanges: Int, totalGraciesMeals: Int) {
        self.totalGraciesMeals = totalGraciesMeals
        self.totalMealExchanges = totalMealExchanges
    }

}

Upvotes: 3

Views: 1588

Answers (3)

Lontronix
Lontronix

Reputation: 193

This turned out to be a bug in SwiftUI. If a list is embedded in a navigation view and has 6 or more sections the app will crash when attempting to present a sheet (FB6818276)

Upvotes: 1

kontiki
kontiki

Reputation: 40639

I tried it on a simulator (iPhone Xr), and it does not crash. I am using Xcode 11 Beta 4. What is your setup?

I did spot something that is wrong though. You are calling willChange in didSet, but you should really call it before that change is made, in willSet. You should change:

var totalMealExchanges: Int {didSet {willChange.send()}}
var totalGraciesMeals: Int {didSet {willChange.send()}}

to:

var totalMealExchanges: Int {willSet {willChange.send()}}
var totalGraciesMeals: Int {willSet {willChange.send()}}

Upvotes: 2

SarahR
SarahR

Reputation: 905

I haven’t tested this, but I have had better results with .sheet by attaching it to a parent view. Try moving it so that it is a modifier of the outer VStack instead of the MealView.

Upvotes: 0

Related Questions