如果格式化代码(选择并使用
ctrl-i
),您将看到
buttonPressed
函数实际上是
外部
您的
ContentView
结构。将其移到内部,它将正确编译:
struct ContentView: View {
// defined global variables in a function
@State var backgroundColor: Color = Color.red
var body: some View {
ZStack {
// background called
backgroundColor
.edgesIgnoringSafeArea(.all)
// content
contentLayer
}
}
var contentLayer: some View {
// content
VStack {
Text("Welcome to newsapptest!")
.font(.largeTitle)
Button(action: {
buttonPressed()
}, label: {
Text("Click here to continue to the app!")
.font(.headline)
.foregroundColor(.white)
.padding()
.background(Color.black)
.cornerRadius(10)
})
}
}
func buttonPressed() {
backgroundColor = .blue
}
}