0

I have boolean variable (isTapped), once the app is launched, I'am giving it a true value, once it moves to (signupView) the app should test the value inside if statement to show some text and after that the app should change the value of (isTapped) to false!!

But it is not working!! as Type () cannot confirm to view. I tried to create a function and call it inside a view but it is not working either! Here is my code:

import SwiftUI

struct SignupView: View {
@AppStorage("isTapped") var isTapped : Bool?
var body: some View {
    if (isTapped == true){
        Text ("signup")
        isTapped = false 
        
    }
    else {
        Text ("Splash")
    }

}
}
5
  • You can’t set things in body Commented Mar 7, 2022 at 9:51
  • That code doesn’t make sense, you only have a Text component and nothing that can change the state in any way. You need a button and/or TextField and set the property to false when they are used Commented Mar 7, 2022 at 9:53
  • to be more clear once I launch the app (isTapped) is true so I want to view some Texts! however when the app is already on the user device I will view some other Texts!! so I used @AppStorage("isTapped") var isTapped = true to give it a true value and once I show (signup) text, and have the app on my device I want to show only (splash) text!! @JoakimDanielson Commented Mar 7, 2022 at 10:52
  • Then that is the change of state I am referring to. Not sure exactly what you mean with “app is on device” but I assume you mean when a specific view is showing so using the .onAppear modifier to set the variable to false is probably a good way forward Commented Mar 7, 2022 at 11:07
  • "app is on device" I mean by that when the user already have the app on his device Commented Mar 7, 2022 at 11:18

2 Answers 2

1

If you want Text("signup") should be shown when isTapped is True, you need additional variable to keep the origin value

struct SignupView: View {
    @AppStorage("isTapped") var isTapped : Bool = false
    @State var isTappedOrigin: Bool = false
    
    var body: some View {
        Group {
            if (isTappedOrigin == true){
                Text ("signup")
            } else {
                Text ("Splash")
            }
        }.onAppear {
            isTappedOrigin = isTapped
            isTapped = false
        }
    }
}

You need to set isTapped = true somewhere to see Text("signup") like this

struct ContainView: View {
    @AppStorage("isTapped") var isTapped : Bool = false
    @State var show: Bool = false
    var body: some View {
        List {
            Toggle("isTapped", isOn: $isTapped)
            Button("show signup view") {
                show.toggle()
            }
        }
        .sheet(isPresented: $show) {
            SignupView()
        }
    }
}

If you want to show signup only once when launch app it should be like this

struct TestView: View {
    @AppStorage("isSignedUp") var isSignedUp : Bool = false
    @State var shouldShowSignUp : Bool = false
    
    var body: some View {
        Group {
            if shouldShowSignUp {
                Text("Sign Up")
            } else {
                Text("Splash")
            }
        }.onAppear {
            shouldShowSignUp = !isSignedUp
            isSignedUp = true
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have added this part to my code but the result always "Splash" :(
please check my answer again
Thank you sooo much !! the last code worked like a charm!! but I am wondering why the first code that you have provided did not work although its quite the same!
it's just basic logic, sooner or later you will get it.
1

You can't set anything to a variable inside the view hierarchy. So to achieve what you want to do, maybe you could try this:

var body: some View {
    if (isTapped == true){
        Text ("signup")
            .onAppear {
                isTapped = false 
            }
    } else {
        Text ("Splash")
    }
}

onAppear closure is run outside the view hierarchy, so you can set values to a variable.

**Update: Re: your comment, that's how SwiftUI works. When isTapped = false is set in onAppear, SwiftUI immediately reloads the body and the splash will be shown. I think to achieve what you need, you shouldn't use @AppStorage. It will cause an update of the body when it's updated. Why don't you just use UserDefaults like this?:

let isTapped: Bool? = UserDefaults.standard.bool(forKey: "isTapped")

...

Text ("signup")
    .onAppear {
        UserDefaults.standard.set(false, forKey: "username") 
    }

5 Comments

I have added (.onAppear) but the result is always ("Splash") that means the app only executed (.onAppear) and changed (isTapped) value to false! but Text("signup") did not execute when (isTapped) is true!!
@HaneenOqailan Just updated the answer. Please have a look
I have added the above requested code but still getting (Splash)!! Couldn't get "sign up" for even once however I want to show it once only when I launch the app!
@HaneenOqailan It's because you didn't set UserDefaults = true anywhere. Maybe if you replace if (isTapped == true) with if (isTapped == nil), it will work.
Similarly, if you handle the splash in if isTapped == false and handle the signup in else, it will also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.