Home > Mobile >  Passing data to .popover in swiftui
Passing data to .popover in swiftui

Time:06-01

I have a String value that changes after pressing the button, and I have a .popover to show it's value, but the value is not changing. I've added print() on each step to clearly show that the value had changed

My ContentView.swift

import SwiftUI

struct ContentView: View {
    @State private var showingPopover = false
    @State private var popoverText = "hey, I'm the initial text"

    var body: some View {
        Button("press me!") {
            print("1) \(popoverText)")
            popoverText = "hey, I'm the new text"
            print("2) \(popoverText)")
            showingPopover = true
            print("3) \(popoverText)")
                }
                .popover(isPresented: $showingPopover) {
                    Text(popoverText)
                        .onAppear(perform: {print("4) \(popoverText)")})
                }
    }
}

After pressing the button it prints out this:

1) hey, I'm the initial text
2) hey, I'm the new text
3) hey, I'm the new text
4) hey, I'm the new text

And shows this: text is not changed

Though I've found a weird workaround. Everything works fine if I add Text(popoverText):

import SwiftUI

struct ContentView: View {
    @State private var showingPopover = false
    @State private var popoverText = "hey, I'm the initial text"

    var body: some View {
        VStack{
            Text(popoverText)
            Button("press me!") {
                print("1) \(popoverText)")
                popoverText = "hey, I'm the new text"
                print("2) \(popoverText)")
                showingPopover = true
                print("3) \(popoverText)")
                    }
            }
                .popover(isPresented: $showingPopover) {
                    Text(popoverText)
                        .onAppear(perform: {print("4) \(popoverText)")})
                }
    }
}

It prints the same thing:

1) hey, I'm the initial text
2) hey, I'm the new text
3) hey, I'm the new text
4) hey, I'm the new text

But now it shows the new text: enter image description here

I think it has something to do with view refreshing or focus but I had no success in nailing down the exact thing

EDIT: my actual application is not with the button, code below is just made as an example so keeping .isPresented is necessary, and it's only allowed to use either .isPresented or .item

CodePudding user response:

.popover(item: someIdentifiableObj, content: ...) is use to pass data into popover

Preview

struct HomeView: View {
    @State var userObj: User?

    var body: some View {
        Button("press me!") {
            userObj = User(popoverText: "New Text")
        }
        .popover(item: $userObj) { user in
            Text(user.popoverText)
        }
    }
}

// MARK: - PREVIEW

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

struct User: Identifiable {
    let id: UUID = UUID()
    let popoverText: String
}
  • Related