2

我想在按钮点击等事件上显示 ActionSheet(或任何其他模式,但 Alert)。

我找到了使用状态变量的方法。以这种方式显示它对我来说似乎有点奇怪,因为我必须在 ActionSheet 手动关闭时重置变量。

有更好的方法吗?

为什么有一个单独的方法来呈现警报,允许您将其可见性绑定到状态变量?我的方法有什么不同?

struct Sketch : View {
    @State var showActionSheet = false

    var body: some View {
        ZStack {
            Button(action: { showActionSheet = true }) { Text("Show") }
        }
        .presentation(showActionSheet ?
            ActionSheet(
                title: Text("Action"),
                buttons: [
                    ActionSheet.Button.cancel() { 
                        self. showActionSheet = false 
                    }
                ]) 
        : nil)
    }
}
4

4 回答 4

1

强制执行状态变量方法的偏好,Apple 采用了警报和操作表 API。为了其他发现此问题的人的利益,这里是基于 Xcode 11 beta 7、iOS 13 的所有 3 种类型的更新示例。

@State var showAlert = false
@State var showActionSheet = false
@State var showAddModal = false

var body: some View {
    VStack {
        // ALERT
        Button(action: { self.showAlert = true }) {
            Text("Show Alert")
        }
        .alert(isPresented: $showAlert) {
             // Alert(...)
             // showAlert set to false through the binding
        }

        // ACTION SHEET
        Button(action: { self.showActionSheet = true }) {
            Text("Show Action Sheet")
        }
        .actionSheet(isPresented: $showActionSheet) {
             // ActionSheet(...)
             // showActionSheet set to false through the binding
        }

        // FULL-SCREEN VIEW
        Button(action: { self.showAddModal = true }) {
            Text("Show Modal")
        }
        .sheet(isPresented: $showAddModal, onDismiss: {} ) {
            // INSERT a call to the new view, and in it set showAddModal = false to close
            // e.g. AddItem(isPresented: self.$showAddModal)
        }
}
于 2019-09-08T23:07:32.510 回答
0

对于您问题的模态部分,您可以使用PresentationButton

struct ContentView : View {
    var body: some View {
        PresentationButton(Text("Click to show"), destination: DetailView())
    }
}

资源

于 2019-06-06T17:46:08.230 回答
0
struct Sketch : View {
    @State var showActionSheet = false

    var body: some View {
    ZStack {
        Button(action: { self.showActionSheet.toggle() }) { Text("Show") }
            .actionSheet(isPresented: $showActionSheet) {
                ActionSheet(title: Text("Test"))
            }
        }
    }
}

这会起作用,@State 是一个属性包装器,您的操作表会密切关注它,只要它变为 true,就会显示操作表

于 2019-10-04T12:46:42.303 回答
0

以下是根据要求显示多个操作表的最佳方式。

struct ActionSheetBootCamp: View {

@State private var isShowingActionSheet = false
@State private var sheetType: SheetTypes = .isOtherPost

enum SheetTypes {
    case isMyPost
    case isOtherPost
}

var body: some View {
    HStack {
        Button("Other Post") {
            sheetType = .isOtherPost
            isShowingActionSheet.toggle()
        }
        Button("My post") {
            sheetType = .isMyPost
            isShowingActionSheet.toggle()
        }
    }.actionSheet(isPresented: $isShowingActionSheet, content: getActionSheet)
}

func getActionSheet() -> ActionSheet {
    
    let btn_share: ActionSheet.Button = .default(Text("Share")) {
        //Implementation
    }
    let btn_report: ActionSheet.Button = .destructive(Text("Report")) {
        //Implementation
    }
    let btn_edit: ActionSheet.Button = .default(Text("Edit")) {
        //Implementation
    }
    let btn_cancel: ActionSheet.Button = .cancel()
    switch(sheetType) {
    case .isMyPost:
        return ActionSheet(title: Text("This is the action sheet title"), message: nil, buttons: [btn_share,btn_edit, btn_cancel])
    case .isOtherPost:
        return ActionSheet(title: Text("This is the action sheet title"), message: nil, buttons: [btn_share,btn_report, btn_cancel])
    }
    
}
}
于 2021-11-20T09:08:42.573 回答