在SwiftUI中将ActionSheet / Sheet转换为模态

时间:2019-08-20 01:33:42

标签: swift swiftui

我正在尝试创建一个actionSheet来打开一个模式以保存到视图。当前,我只能获得actionSheet中的按钮之一才能打开模式。如何重写.sheet代码以执行此操作?谢谢。

@State var showActionSheet = false
@State var showAddActivityPopup = false
@State var showAddDayPopup = false

var body: some View {
    Button(action: {
        self.showActionSheet = true
    }) {
        Text("Show Action Sheet")
    }

    .actionSheet(isPresented: $showActionSheet, content: actionSheet)
        .sheet(isPresented: $showAddDayPopup, onDismiss: {
            print(self.showActionSheet)
        }) {
            AddDayPopup(message: "Add Day")
    }


    .sheet(isPresented: $showAddActivityPopup, onDismiss: {
        print(self.showActionSheet)
    }) {
        AddActivityPopup(message: "Add Activity")
    }
    }




private func actionSheet() -> ActionSheet {
    let button1 = ActionSheet.Button.default(Text("Add Day")) {
        self.showActionSheet = false
        self.showAddDayPopup = true
    }

    let button2 = ActionSheet.Button.default(Text("Add Activity")) {
        self.showActionSheet = false
        self.showAddActivityPopup = true
    }

    let actionSheet = ActionSheet(title: Text("What would you like to add?"),
                                  message: nil,
                                  buttons: [button1, button2, .cancel()])
    return actionSheet
}

}

0 个答案:

没有答案