Hello! I was trying to code something basic after going through the 14 day masterclass but I keep getting this error. Not sure what the problem is
You need to return something from your happyHalloween
function.
Like so:
func happyHalloween() -> some View {
ZStack {
Image("background")
HStack {
Image("stars")
}
}
}
Iām not sure what youāre intending here, though, as you are calling happyHalloween()
as part of the action clause of a Button
. Are you trying to show this ZStack
when the button is pressed?
Ohh okay, thank you so much! sorry, i made that change but another warning popped up where I call on the happyHalloween function which states āResult of call to āhappyHalloween()ā is unusedā. Could you please help me with that as well? and also, yes, Iām trying to get an image to show up once the button is pressed.
Okay, Iām so sorry but the update is that I fixed the error (by adding _= to the front as I saw somewhere online) but when I run it using the simulator, the pictures donāt show up?
So the way to do this is to just include your ZStack
in the body
of your View
, but with an .opacity(_:)
modifier attached. The parameter of the modifier should be an @State
var that is changed in the action of the Button
.
Something like this:
struct HalloweenView: View {
@State private var showHalloweenMessage = false
var body: some View {
ZStack {
Button {
showHalloweenMessage.toggle()
} label: {
Text("Click Here")
.font(.title2)
.fontWeight(.medium)
.underline()
.foregroundColor(.orange)
}
ZStack {
Image("background")
HStack {
Image("stars")
}
}
.opacity(showHalloweenMessage ? 1 : 0)
}
}
}
If you want to factor that ZStack
out, do it in a computed var:
struct HalloweenView: View {
@State private var showHalloweenMessage = false
var body: some View {
ZStack {
Button {
showHalloweenMessage.toggle()
} label: {
Text("Click Here")
.font(.title2)
.fontWeight(.medium)
.underline()
.foregroundColor(.orange)
}
happyHalloween
.opacity(showHalloweenMessage ? 1 : 0)
}
}
var happyHalloween: some View {
ZStack {
Image("background")
HStack {
Image("stars")
}
}
}
}
When posting code to these forums, please post the code as text rather than an image. This makes it far easier to read (some of us have aging eyes!) and also makes it possible for other posters to copy/paste the code in order to test solutions and such without having to retype all your code from scratch.
To post your code as text, place three backticks ```
on the line before your code and three backticks ```
on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </>
button on the toolbar to wrap the block for you, athough itās usually better to put the backticks before you paste in your code.
Iām sorry, will post the code from now on! and Iām sorry for continuing to bother you but I put that code in and though there are no errors, nothing is displaying. I do have some experience in java so it seems like itās because the happyHalloween variable that holds the ZStack isnāt being called upon? but Iām not sure.
import SwiftUI
struct HalloweenView: View {
@State private var showHalloweenMessage = false
var body: some View {
ZStack {
Button {
showHalloweenMessage.toggle()
} label: {
Text("Click Here")
.font(.title2)
.fontWeight(.medium)
.underline()
.foregroundColor(.orange)
}
.opacity(showHalloweenMessage ? 1 : 0)
}
}
var happyHalloween: some View {
ZStack {
Image("background")
HStack {
Image("stars")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
HalloweenView()
}
}
You donāt show happyHalloween
anywhere in the body
of your View
. Compare my code to yours and spot the difference.
Also, you used the HalloweenView
name that I used, but your preview struct is still called ContentView_Previews
. Make sure that your app struct is calling the correct View
.
Oh my god it finally worked, thank you so so much!!