简体   繁体   English

SwiftUI:如何在单击 ActionSheet 按钮时显示新视图?

[英]SwiftUI: How to present new views on clicking ActionSheet button?

I have an action sheet consist of three actions我有一个包含三个动作的动作表

  1. Action_1行动_1
  2. Action_2行动_2
  3. Cancel取消

If I tap on 'Action_1', My app should present ActionView_1().如果我点击“Action_1”,我的应用程序应该显示 ActionView_1()。 If I tap on 'Action_2', My app should present ActionView_2().如果我点击“Action_2”,我的应用程序应该显示 ActionView_2()。

I got this for presenting a view我得到这个是为了展示一个观点

.sheet(isPresented: $isAddSecretVisible){ActionView_1()} . .sheet(isPresented: $isAddSecretVisible){ActionView_1()}

But this for presenting a view with a Button click.但这是为了通过单击按钮来呈现视图。

I need the same action if tap on the Actionsheet button.如果点击 Actionsheet 按钮,我需要相同的操作。 Answer needs in SwiftUISwiftUI中回答需求

Thanks in advance.提前致谢。

You should define 2 State , each for each a sheet:您应该定义 2 个State ,每个对应一张工作表:

@State var isMainActionPresented = false
@State var isActionViewPresented = false

And a State to determine witch actionSheet to present.并确定要呈现的女巫actionSheetState So you can have an enum for that like:所以你可以有一个这样的枚举:

enum ActionViewMode {
    case first
    case second
}

And a helper extension on that:还有一个辅助扩展:

extension ActionViewMode {
    var view: some View {
        switch self {
            case .first: return ActionView1()
            case .second: return ActionView2()
        }
    }
}

Then on click of any Button or ActionSheet.Button , toggle the desired state.然后单击任何ButtonActionSheet.Button ,切换所需的状态。 Look at the complete ContentView code below:看看下面完整的ContentView代码:

@State var actionViewMode = ActionViewMode.first

@State var isMainActionPresented = false
@State var isActionViewPresented = false

var body: some View {
    Button(action: {
        self.isMainActionPresented = true
    }) {
        Text("ActionSheet")
    }
    .actionSheet(isPresented: $isMainActionPresented) {
        ActionSheet(
            title: Text("Title"),
            message: Text("Message"),
            buttons: [
                .default(
                    Text("Action_1"),
                    action: {
                        self.actionViewMode = .first
                        self.isActionViewPresented = true
                }),
                .default(
                    Text("Action_2"),
                    action: {
                        self.actionViewMode = .second
                        self.isActionViewPresented = true
                }),
                .cancel()
        ])
    }
    .sheet(isPresented: $isActionViewPresented) {
        self.actionViewMode.view
    }
}

SwiftUI will handle the rest. SwiftUI 将处理剩下的事情。

Note that you can't chain multiple sheets one after another, because each one overrides previous somehow.请注意,您不能一个接一个地链接多个工作表,因为每个工作表都会以某种方式覆盖之前的工作表。

Reference link: SwiftUI参考链接: SwiftUI

ActionSheet(title: Text("iOSDevCenters"), message: Text("SubTitle"), buttons: [
            .default(Text("Action_1"), action: {
                                print("Action_1")
                                ActionView_1()
                            }),
            .destructive(Text("Cancel"))
         ])
})

I have an action sheet consist of three actions我有一个包含三个动作的动作表

  1. Action_1行动_1
  2. Action_2行动_2
  3. Cancel取消

If I tap on 'Action_1', My app should present ActionView_1().如果我点击“Action_1”,我的应用应该显示 ActionView_1()。 If I tap on 'Action_2', My app should present ActionView_2().如果我点击“Action_2”,我的应用应该显示 ActionView_2()。

I got this for presenting a view我得到这个是为了展示一个观点

.sheet(isPresented: $isAddSecretVisible){ActionView_1()} . .sheet(isPresented: $isAddSecretVisible){ActionView_1()}

But this for presenting a view with a Button click.但这用于通过按钮单击呈现视图。

I need the same action if tap on the Actionsheet button.如果点击 Actionsheet 按钮,我需要相同的操作。 Answer needs in SwiftUI SwiftUI中的回答需求

Thanks in advance.提前致谢。

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

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