How to present an Alert with swiftUI

Luk*_*ger 10 ios swift ios13 swiftui

In swiftUI I discovered the Alert type. But I wonder how to show it with the presentation method.

Initializing an Alert is pretty easy. But how to use the binding?

struct ContentView : View {
    var body: some View {
        Button(action: {
            // Don't know how to use the `binding` below
            presentation(binding, alert: {
                Alert(title: Text("Hello"))
            })
        }, label: {
            Text("asdf")
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

The binding is of type Binding<Bool>

thi*_*oxe 13

.presentation()在Beta 4中实际上已弃用。这是当前与.alert()修改器一起使用的版本。

struct ContentView: View {
    @State var showsAlert = false
    var body: some View {
        Button(action: {
            self.showsAlert.toggle()
        }) {
            Text("Show Alert")
        }
        .alert(isPresented: self.$showsAlert) {
            Alert(title: Text("Hello"))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我们如何在 Helper 中使用它?因为我们必须多次调用警报。 (2认同)

ram*_*nok 10

这是显示多个警报的解决方案。适用于iOS13-iOS15

struct YourView: View {
    enum AlertType: Identifiable {
        case first, second
        
        var id: Int {
            hashValue
        }
    }
    
    @State var alertType: AlertType?
    
    var body: some View {
        VStack {
            Button("Show alert #1") {
                alertType = .first
            }
            
            Button("Show alert #2") {
                alertType = .second
            }
        }
        .alert(item: $alertType) { type in
            switch type {
            case .first:
                return Alert(title: Text("First alert"))
            case .second:
                return Alert(title: Text("Second alert"))
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


tsp*_*tsp 7

您可以使用@State变量作为绑定。或者,您可以使用使用的@EnvironmentObject变量BindableObject

我认为你需要调用presentation根视图得到它的工作,将其添加到StackGroup等似乎并不工作。

这个片段似乎可以解决问题。请注意,@State解除警报后,变量设置为false。

struct ContentView: View {

    @State var showsAlert = false

    var body: some View {
        Button(action: {
            self.showsAlert = true
        }, label: {
            Text("asdf")
        }).presentation($showsAlert, alert: {
            Alert(title: Text("Hello"))
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这个答案不再正确,因为 SwiftUI 中已弃用“.presentation”。 (4认同)

Kir*_*odi 7

完整的警报代码,具有撤消和可采取的措施:

码:

import SwiftUI

struct ContentView: View {
    @State private var isAlert = false

    var body: some View {
            Button(action: {
                self.isAlert = true
            }) {
                Text("Click Alert")
                .foregroundColor(Color.white)
            }
            .padding()
            .background(Color.blue)
            .alert(isPresented: $isAlert) { () -> Alert in
                Alert(title: Text("iOSDevCenters"), message: Text("This Tutorial for SwiftUI Alert."), primaryButton: .default(Text("Okay"), action: {
                    print("Okay Click")
                }), secondaryButton: .default(Text("Dismiss")))
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Output


Mo *_*ani 5

除了@tsp 的回答,要显示带有两个按钮的警报并处理按钮点击动作,您可以执行以下操作:

@State var showAlert = false

var body: some View {
  Button(action: {
    self.showAlert = true
  }) {
    Text("Show Alert")
  }
  .presentation($showAlert) {
      Alert(title: Text("Title"), message: Text("Message..."),
          primaryButton: .default (Text("OK")) {
            print("OK button tapped")
          },
          secondaryButton: .cancel()
      )
  }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明