SwiftUI在一个视图中显示多个警报

wvt8vs2t  于 2024-01-05  发布在  Swift
关注(0)|答案(2)|浏览(154)

我有2警报在1视图这样(* 这些警报做个别工作 *),但当结合2警报到1视图其只显示警报2.
我读到你需要把这些附加到不同的观点。
因此,我已经将1警报附加到button,并将1警报附加到包含VStack。仍然只有第二个警报显示。我试图让这两个警报工作。

  1. var body: some View {
  2. ScrollView {
  3. VStack (alignment: .leading) {
  4. ...some stuff
  5. VStack {
  6. Button(action: {
  7. dosomestuff
  8. showingIntrestedAlert.toggle()
  9. }) {
  10. Text("Press Me")
  11. }.alert(isPresented: $showingIntrestedAlert) {
  12. Alert(title: Text("alert1"), message: Text("showing alert 1"), dismissButton: .default(Text("OK")))
  13. }
  14. }
  15. }
  16. .alert(isPresented: $fromViewModel.alreadyLikedUser) {
  17. Alert(title: Text("alert2"), message: Text("alert 2 shown"), dismissButton: .default(Text("OK")))
  18. }
  19. }
  20. }

字符串

cnh2zyt3

cnh2zyt31#

你可以插入多个警报消息在这下面的方式.
首先:为您想要处理的每个警报减少变量。

  1. struct xyz : View
  2. {
  3. @State private var myFirstAlert : Bool = false
  4. @State private var mySecondAlert : Bool = false
  5. ....
  6. }

字符串
在你的身体里写下你的纽扣像这样

  1. var body: some View {
  2. VStack{
  3. Text("Hello World")
  4. Spacer()
  5. ....
  6. Button {
  7. guard someThingIsEmpty else{
  8. myFirstAlert = true
  9. return
  10. }
  11. guard somethingIsWrong else {
  12. mySecondAlert = true
  13. return
  14. }
  15. //Button stuff
  16. } label: { ZStack {Text("Bla"),....}} .alert(isPresented: self.$myFirstAlert){
  17. Alert(title: Text("Something went wrong!"))}
  18. .alert(isPresented: self.$mySecondAlert){
  19. Alert(title: Text("That was shitty"), message: Text("Something went totally wrong"), dismissButton: .default(Text("Urghs"), action: {}}

展开查看全部
gtlvzcf8

gtlvzcf82#

自定义提醒

您只需要创建一个自定义警报,以在单个视图中显示多个警报。

  1. struct CustomAlertView<Content>: View where Content: View {
  2. @Binding var isPresented: Bool
  3. var onConfirm: () -> Void
  4. var title : String
  5. var message : String
  6. let content: () -> Content
  7. var body: some View {
  8. Group {
  9. if isPresented {
  10. content()
  11. .alert(isPresented: $isPresented) {
  12. Alert(
  13. title: Text(title),
  14. message: Text(message),
  15. primaryButton: .cancel(Text("Yes"), action: {
  16. onConfirm()
  17. }),
  18. secondaryButton: .default(Text("No"), action: {})
  19. )
  20. }
  21. } else {
  22. content()
  23. }
  24. }
  25. }
  26. }

字符串

Usage:

  1. struct HomeView: View {
  2. @State var alert1 : Bool = false
  3. @State var alert2 : Bool = false
  4. var body: some View {
  5. VStack {
  6. Spacer()
  7. Text("Custom Alerts")
  8. Button(action: {
  9. alert1 = true
  10. }) {
  11. Text("Alert 1")
  12. }
  13. Button(action: {
  14. alert2 = true
  15. }) {
  16. Text("Alert 2")
  17. }
  18. Spacer()
  19. }
  20. .overlay(
  21. CustomAlertView(isPresented: $alert1, onConfirm: {
  22. }, title: "Custom Alert 1", message: "This is Alert 1.") {
  23. Text("")
  24. }
  25. )
  26. .overlay(
  27. CustomAlertView(isPresented: $alert2, onConfirm: {
  28. }, title: "Custom Alert 2", message: "This is Alert 2.") {
  29. Text("")
  30. }
  31. )
  32. }
  33. }

展开查看全部

相关问题