ios SwiftUI -显示来自ToolbarItem内部按钮的警报

nfg76nw0  于 2023-06-25  发布在  iOS
关注(0)|答案(2)|浏览(141)

我想做一些类似于下面代码的事情,当用户点击导航栏项目按钮时,它会显示一个警报。但是下面的代码不起作用,警报不会显示。
我不能将alert修饰符添加到NavigationView中,因为我的实际应用程序更复杂,而VStack是另一个文件中的视图,在同一个视图中添加多个alert修饰符也不起作用(只有最后一个添加的有效)。

import SwiftUI

struct SOQuestionView: View {
    @State private var showAlert1 = false
    @State private var showAlert2 = false

    var body: some View {
        NavigationView {
            VStack {
                Text("Click button in toolbar")
            }
            .navigationBarTitle(Text("Title"))
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button(action: {
                        showAlert1 = true
                    }) {
                        Image(systemName: "square.and.arrow.up")
                    }
                    .alert(isPresented: $showAlert1) {
                        Alert(title: Text("Alert 1"))
                    }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        showAlert2 = true
                    }) {
                        Image(systemName: "square.and.arrow.up.fill")
                    }
                    .alert(isPresented: $showAlert2) {
                        Alert(title: Text("Alert 2"))
                    }
                }
            }
        }
    }
}

pexxcrt2

pexxcrt21#

解决方案是在工具栏之外使用alert,并使用不同的构造函数。使用Xcode 12.1 / iOS 14.1进行测试。

struct SOQuestionView: View {
    @State private var alertItem: String? // assuming item confirmed to Identifiable
    
    var body: some View {
        NavigationView {
            VStack {
                Text("Click button in toolbar")
            }
            .navigationBarTitle(Text("Title"))
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button(action: {
                        alertItem = "Alert 1"
                    }) {
                        Image(systemName: "square.and.arrow.up")
                    }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        alertItem = "Alert 2"
                    }) {
                        Image(systemName: "square.and.arrow.up.fill")
                    }
                }
            }
            .alert(item: $alertItem) { item in
                Alert(title: Text(item))
            }
        }
    }
}
wa7juj8i

wa7juj8i2#

我用这个视图传递带有alert的按钮,它在ToolbarItem内部工作

struct AlertButton<L: View, M: View, A: View>: View {
    @ViewBuilder let label: () -> L
    let alertTitle: LocalizedStringKey
    @ViewBuilder let alertMessage: () -> M
    @ViewBuilder let actions: () -> A

    @State private var isAlertPresented = false

    var body: some View {
        Button {
            isAlertPresented.toggle()
        } label: {
            label()
        }
        .alert(
            alertTitle,
            isPresented: $isAlertPresented,
            actions: actions,
            message: alertMessage
        )
    }
}

相关问题