简体   繁体   English

swiftui 一个带有 2 或 3 个操作表的视图?

[英]swiftui one view with 2 or 3 actionsheets?

I want to call different action sheets in a view, using a variable.我想使用变量在视图中调用不同的操作表。 That does not mean to work.这并不意味着工作。

.actionSheet(isPresented: self.$neuinitialisierung) {
                ActionSheet(
                    title: Text("Testtitel 1"),
                    message: Text("Testmessage 1"),
                    buttons: [
                        .default(Text("Button 1"),
                                 action: {
                                    print("KLICK")
                        }),
                        .default(Text("Button 2"),
                        action: {
                            self.clouddienst_waehlen = true;
                        })
                        ])
            }
.actionSheet(isPresented: self.$clouddienst_waehlen) {
                ActionSheet(
                    title: Text("Testtitel 2"),
                    message: Text("Testmessage 2"),
                    buttons: [
                        .default(Text("Button 1"),
                                 action: {
                                    print("KLICK")
                        }),
                        .default(Text("Button 2"),
                        action: {
                            self.clouddienst_waehlen = true;
                        })
                        ])
            }

If I try it with just one action sheet, it works.如果我只用一张操作表尝试它,它就可以工作。 How can I use the second?我该如何使用第二个?

I came up with this solution:我想出了这个解决方案:

@State var showingMenu = false
@State var optionsMenu: OptionsMenu = .main

enum OptionsMenu { case main, export }

...

.actionSheet(isPresented: $showingMenu) {
    if self.optionsMenu == .main {
        return ActionSheet(title: Text("Main Menu"), buttons: [
            .default(Text("Export Menu")) {
                delay(0.1) {
                    self.optionsMenu = .export
                    self.showingMenu = true
                }
            }
            .destructive(Text("Close"))
        ])
    } else {
        return ActionSheet(title: Text("Export Menu"), buttons: [
            .default(Text("Export timeline as GPX")) {
                // TODO
            },
            .default(Text("Export timeline as JSON")) {
                // TODO
            },
            .destructive(Text("Close"))
        ])
    }
}

And the button that opens the first menu needs to make sure to reset the enum value, otherwise the wrong menu will open on next button tap:并且打开第一个菜单的按钮需要确保重置枚举值,否则在下一个按钮点击时会打开错误的菜单:

Button(action: {
    self.optionsMenu = .main
    self.showingMenu = true
}) {
    Image(systemName: "ellipsis")
}

You can use:您可以使用:

func actionSheet<T>(item: Binding<T?>, content: (T) -> ActionSheet) -> some View where T : Identifiable

It takes a Binding to some kind of optional and if the value is not nil presents an ActionSheet.它需要绑定到某种可选值,如果值不是 nil,则呈现一个 ActionSheet。 Instead of setting your a flag to true, you would set this optional to some value.您可以将此选项设置为某个值,而不是将您的 a 标志设置为 true。

Thanks for your tip.谢谢你的小费。 I'm honest, I do not quite understand how to do that.老实说,我不太明白该怎么做。 Am also relatively new to swift and swiftui.我对 swift 和 swiftui 也相对较新。 Can you show me a code sample?你能给我看一个代码示例吗? That would be nice.那样就好了。

No need for any of these complicated solutions.不需要任何这些复杂的解决方案。 Just attach your.actionSheets to different views.只需将 your.actionSheets 附加到不同的视图。 It doesn't need to be on the root level.它不需要在根级别。 You can use the.actionSheet modifier on a button for example.例如,您可以在按钮上使用 .actionSheet 修饰符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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