0

I want to have a modal view whenever the avatar is pressed and selected. But when using the binding var, there is no way to know whether it is an empty string..

Codes below:

struct SelectAvatarView: View {
    var role: String
    @State var selectedAvatar: String?
    
    var body: some View {
        NavigationView{
            
            ZStack {
                BackgroundView()

                VStack {
                    TitleTextView(title: "Choose your avatar:")
                    
                    if role == "Parent" {
                        
                        ParentAvatarView(selectedAvatar: $selectedAvatar)
                    }
                    else{
                        ChildAvatarView(selectedAvatar: $selectedAvatar)
                    }
                    Spacer()
                }.padding()
                .sheet(isPresented: !(self.$selectedAvatar.isEmpty) ) { SimpleTestView()}
            }
            
        }
    }
}

Problem is I don't know how to check the binding var $selectedAvatar. no matter what I wrote, errors are:

enter image description here

Cannot convert value of type 'Binding<Bool>' to expected argument type 'Bool'

Help!!! and Thanks!!

  • What do you mean "pressed and selected"? There's nothing in your code that responds to a "press". From your code, it looks like you want to present it only if `selectedAvatar` is not empty, correct? So that would mean that if once the sheet gets dismissed, the system would then set `selectedAvatar` to being empty (assuming you had a custom binding written). Is that really what you want? – jnpdx May 30 '21 at 04:58
  • the trigger of selected and pressed is IRRELEVANT here - any SwiftUI developer can understand that because it is implemented onTapGesture in another view. it is not the issue here. Issue is just: I want to check if the environment var is empty string but have not been successful. – CodeLikeNoonesBusiness May 30 '21 at 05:19
  • I was just asking some questions in order to fill in the gaps, since it's not really a [mre]. I think if you look at my track record here on SO you'll see that I have a pretty good history of answering SwiftUI questions, but I *do* try to understand the situation fully first. The reason that I asked the question about how the binding respond is that it seems like having the sheet clear out the `selectedAvatar` once it's dismissed wouldn't be ideal. So, I was trying to get a better idea of the whole situation as to better provide a solution. – jnpdx May 30 '21 at 05:25
  • General comment, when you use `$variable` you are accessing the binding (that SwiftUI uses when updating the variable) for the variable but when you are using `variable` then it is the value (the string in your case) that you are accessing. And you want to check if the value is empty, not the binding – Joakim Danielson May 30 '21 at 06:50
  • Possible duplicate of [SwiftUI - Invert a boolean binding](https://stackoverflow.com/questions/59474045/swiftui-invert-a-boolean-binding) – pkamb Jun 15 '22 at 03:06

2 Answers2

3

sorry to all. I was being careless.

But I was hurt by the new "arrogance" of the stackOverflow now. One asks question in Stack as last resort, because last thing he/she wants is to shame himself/herself in front of all the other professional and awesome developers with the stupidity - It ain't fun. But when stuck with project, we had to throw face/dignity outside the window. Only because missing details in the question description, (when it is absolutely irrelevant to the problem), I don't think it deserves a "thumb down", let alone a few.

In the long run, this kind of arrogance will hurt the very purpose of having stackOverflow in the first place.

isPresented only takes Binding<Bool>, that's why, providing a Bool won't work. enter image description here

So I found a workaround by using enter image description here

It is working fine now:

//change avatar to struct Avatar (identifiable) 
@State var selectedAvatar: Avatar?

//change to "item" 
.sheet(item: self.$selectedAvatar ){ avatar in AvatarSummaryView(avatar: avatar)}


1

I don't know how your view architecture.

But here you can fix your compile error by bellow code.

.sheet(isPresented: .constant(!(self.selectedAvatar?.isEmpty ?? false)) ) { SimpleTestView()}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52