在下面的代码中,无论我将第二个.actionSheet附加到哪里,我都无法使它们工作.我的理解是,只要他们持有不同的观点,就会奏效,但这一理论似乎并不正确.

struct ContentView: View {

        @State private var showFirstActionSheet = false
        @State private var showSecondActionSheet = false
        
        var body: some View {
            VStack{
                VStack{
                    Button("Show Fist Action Sheet"){
                        showFirstActionSheet = true
                    }
                    .actionSheet(isPresented: $showFirstActionSheet) {
                        ActionSheet(title: Text("First Action Sheet"), message:
                                        Text("Some message!"),
                                    buttons: [
                                        .destructive(Text("Cancel")){
                                            
                                        },
                                        .default(Text("Yes")){
                                            doSomething()
                                        },
                                        .cancel()
                                    ])
                    }
                }
            }
            .actionSheet(isPresented: $showSecondActionSheet) {
                ActionSheet(title: Text("Second Action Sheet"), message:
                                Text("Some message!"),
                            buttons: [
                                .destructive(Text("Cancel")){
                                   
                                },
                                .default(Text("Yes")){
                                    
                                },
                                .cancel()
                            ])
            }
        }
        
        func doSomething(){
            showSecondActionSheet = true
        }
    }

我try 了两个不同的按钮,它的工作,但在我的情况下,我只有一个按钮,触发第一个和第二个是由第一个actionSheet触发.

你知道处理这件事最好的办法是什么吗?

推荐答案

如果你把代码中的所有actionSheet都改成confirmationDialog,它会完全正常工作.actionSheet在iOS 17中被弃用.

您可以使用不同的role来创建常规的Button,而不是使用Alert.Button中的工厂方法.

struct ContentView: View {
    
    @State private var showFirstActionSheet = false
    @State private var showSecondActionSheet = false
    
    var body: some View {
        VStack{
            VStack{
                Button("Show First Action Sheet"){
                    showFirstActionSheet = true
                }
                .confirmationDialog("First Action Sheet", isPresented: $showFirstActionSheet, titleVisibility: .visible) {
                    Button("Cancel", role: .destructive) {
                        
                    }
                    Button("Yes") {
                        doSomething()
                    }
                    Button("Cancel", role: .cancel) {
                        
                    }
                } message: {
                    Text("Some message!")
                }
            }
        }
        .confirmationDialog("Second Action Sheet", isPresented: $showSecondActionSheet, titleVisibility: .visible) {
            Button("Cancel", role: .destructive) {
                
            }
            Button("Yes") {
                
            }
            Button("Cancel", role: .cancel) {
                
            }
        } message: {
            Text("Some message!")
        }
    }
    
    func doSomething(){
        showSecondActionSheet = true
    }
}

Swift相关问答推荐

如何将趋势线添加到线性图中?

如何在Swift中获得与Python相同的HMAC—SHA512签名?

如何使用快速宏用来自函数的关联值来初始化枚举?

如何在HStack中均匀分布视图?

如何在SwiftUI 4+(iOS 16+)中使用新的NavigationStack进行导航

找出touch 点的屏幕亮度

为什么Swift可以在没有足够信息的情况下推断类型?

Swift 数组拆分成重叠的块

如何制作进度加载动画?

在 Swift 中为(递归)扩展提供默认实例参数

如何有条件地依赖桌面与 iOS 的系统库?

使用协议捕获 SwiftUI 视图

如何在 Swift 中将 XPC 自定义类列入白名单?

在 Swift 中,如何查看 Process() 传递给 shell 的字符串?

为什么swiftui中的导航视图栏那么大?

AES 加密和解密

如何更改弹出框的大小

Swift 3:日期与 NSDate?

try 在解除分配时加载视图控制器的视图... UIAlertController

为什么'nil' 与 Swift 3 中的 'UnsafePointer' 不兼容?