在 SwiftUI 中单击按钮时调用函数
Call a function when a button is clicked in SwiftUI
我已经从使用 Obj-C 开发应用程序转向使用 swiftUI。
我有一个按钮,点击它时应该显示弹出视图。按照此 link 显示弹出窗口 https://blog.techchee.com/how-to-create-a-pop-up-view-with-swiftui/
但是点击按钮时没有弹出窗口。这是我的代码
struct Location: View {
var body: some View {
ZStack{
Button(action: {
popUpView()
}, label: {
Text("Select Location")
.frame(minWidth: 0, maxWidth: 500)
.padding()
.background(Color.clear)
.foregroundColor(Color.black)
.font(.custom("Open Sans", size: 18))
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 2)
)
})
}
}
private let choices = ["Apple", "Orange", "Papaya", "Grape", "Star Fruit", "Strawberries", "Raspberries", "Blueberries"].sorted()
func popUpView()-> some View {
VStack (spacing : 10) {
Text("Choices Of Fruits").font(Font.custom("Avenir-Black", size: 18.0))
.position(x:100 , y:400)
List(choices, id:\.self) { Text([=10=]) }
Button(action: {
withAnimation {
}
}, label: {
Text("Close")
})
}
.padding()
.frame(width: 240, height: 300)
.background(Color.white)
.cornerRadius(20)
.shadow(radius: 20 )
}
}
请问哪里出了问题?
根据您的设计,您可以只在主 ZStack 中添加条件并隐藏显示视图。
此外,您可以使用 sheet.
struct Location: View {
@State private var isPresent: Bool = false // <-- Here
var body: some View {
ZStack{
Button(action: {
withAnimation {
isPresent = true // <-- Here
}
}, label: {
Text("Select Location")
.frame(minWidth: 0, maxWidth: 500)
.padding()
.background(Color.clear)
.foregroundColor(Color.black)
.font(.custom("Open Sans", size: 18))
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 2)
)
})
if isPresent { // <-- Here
popUpView()
}
}
}
private let choices = ["Apple", "Orange", "Papaya", "Grape", "Star Fruit", "Strawberries", "Raspberries", "Blueberries"].sorted()
func popUpView()-> some View {
VStack (spacing : 10) {
Text("Choices Of Fruits").font(Font.custom("Avenir-Black", size: 18.0))
.position(x:100 , y:400)
List(choices, id:\.self) { Text([=10=]) }
Button(action: {
withAnimation {
isPresent = false // <-- Here
}
}, label: {
Text("Close")
})
}
.padding()
.frame(width: 240, height: 300)
.background(Color.white)
.cornerRadius(20)
.shadow(radius: 20 )
}
}
我已经从使用 Obj-C 开发应用程序转向使用 swiftUI。 我有一个按钮,点击它时应该显示弹出视图。按照此 link 显示弹出窗口 https://blog.techchee.com/how-to-create-a-pop-up-view-with-swiftui/
但是点击按钮时没有弹出窗口。这是我的代码
struct Location: View {
var body: some View {
ZStack{
Button(action: {
popUpView()
}, label: {
Text("Select Location")
.frame(minWidth: 0, maxWidth: 500)
.padding()
.background(Color.clear)
.foregroundColor(Color.black)
.font(.custom("Open Sans", size: 18))
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 2)
)
})
}
}
private let choices = ["Apple", "Orange", "Papaya", "Grape", "Star Fruit", "Strawberries", "Raspberries", "Blueberries"].sorted()
func popUpView()-> some View {
VStack (spacing : 10) {
Text("Choices Of Fruits").font(Font.custom("Avenir-Black", size: 18.0))
.position(x:100 , y:400)
List(choices, id:\.self) { Text([=10=]) }
Button(action: {
withAnimation {
}
}, label: {
Text("Close")
})
}
.padding()
.frame(width: 240, height: 300)
.background(Color.white)
.cornerRadius(20)
.shadow(radius: 20 )
}
}
请问哪里出了问题?
根据您的设计,您可以只在主 ZStack 中添加条件并隐藏显示视图。 此外,您可以使用 sheet.
struct Location: View {
@State private var isPresent: Bool = false // <-- Here
var body: some View {
ZStack{
Button(action: {
withAnimation {
isPresent = true // <-- Here
}
}, label: {
Text("Select Location")
.frame(minWidth: 0, maxWidth: 500)
.padding()
.background(Color.clear)
.foregroundColor(Color.black)
.font(.custom("Open Sans", size: 18))
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.gray, lineWidth: 2)
)
})
if isPresent { // <-- Here
popUpView()
}
}
}
private let choices = ["Apple", "Orange", "Papaya", "Grape", "Star Fruit", "Strawberries", "Raspberries", "Blueberries"].sorted()
func popUpView()-> some View {
VStack (spacing : 10) {
Text("Choices Of Fruits").font(Font.custom("Avenir-Black", size: 18.0))
.position(x:100 , y:400)
List(choices, id:\.self) { Text([=10=]) }
Button(action: {
withAnimation {
isPresent = false // <-- Here
}
}, label: {
Text("Close")
})
}
.padding()
.frame(width: 240, height: 300)
.background(Color.white)
.cornerRadius(20)
.shadow(radius: 20 )
}
}