的方式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)}) } } }