我以前尝试过将.sheet附加在列表上,并且可以正常工作。但是出于我的项目目的,我想使用一个函数来调用模式警报。
import SwiftUI
struct Battlefield : View {
@State var isModalInputPresented: Bool = false
@State var inputTitle: String = ""
@State var inputMessage: String = ""
@State var inputValue: Int = 0
private func summonModalInput(title: String, message: String, input: Int) {
self.isModalInputPresented.toggle()
self.inputTitle = title
self.inputMessage = message
self.inputValue = input
sheet(isPresented: $isModalInputPresented, content: { InputView(inputTitle: self.$inputTitle, inputMessage: self.$inputMessage, inputValue: self.$inputValue, isPresented: self.$isModalInputPresented)})
}
var body: some View {
summonModalInput(title: "foo", message: "bar", input: 0)
}
}
Run Code Online (Sandbox Code Playgroud)
的方式sheet,actionSheet和alert工作是你将它们连接到子视图,并决定是否要与条件结合显示它们。因此,您可以sheet在该视图的主体中将其附加到子视图,并为其提供条件绑定,以便您在要使用其调用模式的函数中进行更改。
例如,如果要显示带有按钮的工作表:
struct Battlefield : View {
@State var isModalInputPresented: Bool = false
@State var inputTitle: String = ""
@State var inputMessage: String = ""
@State var inputValue: Int = 0
private func summonModalInput(title: String, message: String, input: Int) {
self.isModalInputPresented.toggle()
self.inputTitle = title
self.inputMessage = message
self.inputValue = input
self.isModalInputPresented = true
}
var body: some View {
Button(action: {
summonModalInput(title: "foo", message: "bar", input: 0)
}) {
Text("Tap for an alert!")
}
.sheet(isPresented: $isModalInputPresented,
content: {
InputView(inputTitle: self.$inputTitle, inputMessage: self.$inputMessage, inputValue: self.$inputValue, isPresented: self.$isModalInputPresented)})
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
253 次 |
| 最近记录: |