NEW: Learn to build the incredible iOS 15 Weather app today! >>

Passing a boolean from one view to the next

Forums > SwiftUI

This code immediately below kinda half works, but not totally. I'm only trying to pass a boolean in one direction.

import SwiftUI

struct AddView: View {
    let newItem: Bool

    var body: some View {
        Text(String(newItem))
    }
}

struct ContentView: View {
    @State private var newItem = false
    @State private var showingAdd = false

    var body: some View {
        NavigationView {
            Form {
                Button("Add with true") {
                    newItem = true
                    showingAdd = true
                }
                Button("Add with false") {
                    newItem = false
                    showingAdd = true
                }

            }
            .sheet(isPresented: $showingAdd) {
                AddView(newItem: newItem)
            }
        }
    }
}

The code below achieves the desired result because it's using a class which both views can access, but I'm confused why the ContentView can't pass the value to the AddView using the code above.

import SwiftUI

class Test: ObservableObject {
    var newItem: Bool
    init(newItem: Bool) {
        self.newItem = newItem
    }
}

struct AddView: View {
    @ObservedObject var test: Test

    var body: some View {
        Text(String(test.newItem))
    }
}

struct ContentView: View {
    @State private var showingAdd = false
    @StateObject var test = Test(newItem: false)

    var body: some View {
        NavigationView {
            Form {
                Button("Add with true") {
                    test.newItem = true
                    showingAdd = true
                }
                Button("Add with false") {
                    test.newItem = false
                    showingAdd = true
                }
            }
            .sheet(isPresented: $showingAdd) {
                AddView(test: test)
            }
        }
    }
}

Could someone please help me?

   

I think it's just SwiftUI getting a bit confused. If you change your code to this, it works:

NavigationView {
    Form {
        Button("Add with true") {
            newItem = true
        }
        Button("Add with false") {
            newItem = false
        }

    }
    .onChange(of: newItem) { _ in
        showingAdd = true
    }
    .sheet(isPresented: $showingAdd) {
        AddView(newItem: newItem)
    }
}

In this code, you are changing the state of newItem but pushing off the change of showingAdd until just a wee bit later, which seems to make the difference.

   

***Yep. That works great. Much cleaner than my fix. Thanks.

Ok, so that doesn't quite work, or it doesn't achieve what I was hoping. Initially, it does but if you press the button for true, dismiss the sheet and press the button for true it doesn't launch AddView the 2nd or 3rd or nth time. It only launches when you switch back and forth between true and false.

import SwiftUI

struct AddView: View {
    let newItem: Bool

    var body: some View {
        Text(String(newItem))
    }
}

struct ContentView: View {
    @State private var showingAdd = false
    @State private var newItem = false

    var body: some View {
        NavigationView {
            Form {
                Button("Add with true") {
                    newItem = true
                }
                Button("Add with false") {
                    newItem = false
                }
            }
            .onChange(of: newItem) { _ in
                showingAdd = true
            }

            .sheet(isPresented: $showingAdd) {
                AddView(newItem: newItem)
            }
        }
    }
}

   

Ok, so using a small modification to your suggestion, this works:

import SwiftUI

struct AddView: View {
    let newItem: Bool

    var body: some View {
        Text(String(newItem))
    }
}

struct ContentView: View {
    @State private var showingAdd = false
    @State private var newItem = false
    @State private var otherItem = 0

    var body: some View {
        NavigationView {
            Form {
                Button("Add with true") {
                    newItem = true
                    otherItem += 1
                }
                Button("Add with false") {
                    newItem = false
                    otherItem += 1
                }
            }
            .onChange(of: otherItem) { _ in
                showingAdd = true
            }

            .sheet(isPresented: $showingAdd) {
                AddView(newItem: newItem)
            }
        }
    }
}

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.