简体   繁体   中英

How can I invoke a SwiftUI modal sheet through a function/method?

I've previously tried appending .sheet on a list and it worked. But for the purpose of my project, I want to invoke a modal alert using a function.

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)
    }
}

The way sheet , actionSheet , and alert work is that you attach them to a child view, and decide whether or not to show them with a conditional binding. So, you can attach the sheet to a child view in this view's body, and give it a conditional binding that you change in the function you want to invoke the modal with.

For example, if you want to present a sheet with a button:

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)})
        }
    }
}

Here's a second example, a bit more automatic as long as you wish to execute when a view appears:

var body: some View {
    ...
}
.onAppear { self.summonModalInput(title: "foo", message: "bar", input: 0) }
.alert(isPresented: $isModalInputPresented { InputView(inputTitle: self.$inputTitle, inputMessage: self.$inputMessage, inputValue: self.$inputValue, isPresented: self.$isModalInputPresented)}

I like the answer given by @RPatel99 but this one - if it meets your needs - will work also.

The thing to remember is (a) SwiftUI is more a "reactive" platform, (b) your View needs output, and (c) you can only execute functions from a View - like a Button or a modifier to a View.

Last, if you set your model up right, you can execute a function there, set a flag, bind your view to the model and show a sheet based on that flag. This is likely the preferable route to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM