如何使用swiftUI呈现警报

时间:2019-06-04 17:02:42

标签: ios swift swiftui ios13

swiftUI中,我发现了Alert类型。但是我不知道如何使用presentation方法来显示它。

初始化Alert非常简单。但是如何使用绑定?

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")
        })
    }
}

绑定的类型为Binding<Bool>

9 个答案:

答案 0 :(得分: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"))
        }
    }
}

答案 1 :(得分:9)

您可以使用@State变量作为绑定。另外,您可以使用使用@EnvironmentObject的{​​{1}}变量。

我认为您需要在根视图上调用BindableObject才能使其正常运行,然后将其添加到presentationStack等中,似乎不起作用。 / p>

此片段似乎可以解决问题。请注意,解除警报后,Group变量将设置为false。

@State

答案 2 :(得分:7)

Full Code of Alert with dismiss and okay action:

代码:

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()
    }
}

输出:

Output

答案 3 :(得分:2)

带有onTapGesture

的示例
struct MyRow: View {
    @State private var showingAlert = false

    var body: some View {
        HStack {
            Text("Hello")
            Text("World")
        }
        .onTapGesture {
            self.showingAlert = true
        }
        .alert(isPresented: $showingAlert, content: {
            Alert(title: Text("Title"), message: Text("Message"), dismissButton: .default(Text("OK")))
        })
    }
}

答案 4 :(得分:2)

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

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"))
            }
        }
    }
}

答案 5 :(得分:0)

struct ContentView: View {

    @State var aAlert = false

    var body: some View {
        Text("Alert").tapAction {
            self.aAlert = true
        }.presentation($aAlert, alert:{ Alert(title: Text("Alert"))})
    }
}

答案 6 :(得分:0)

除了@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()
      )
  }
}

结果:

enter image description here

答案 7 :(得分:0)

SwiftUI

首先创建基本警报:

Alert(title: Text("Alert title"), message: Text("Alert message"), dismissButton: .default(Text("Got it!")))

然后定义一个可绑定的条件,该条件告诉警报何时可见。切换该条件以显示/隐藏警报。

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

    var body: some View {
        Button(action: {
            self.showingAlert = true
        }) {
            Text("Show Alert")
        }
        .alert(isPresented: $showingAlert) {
            Alert(title: Text("Important message"), message: Text("Wear sunscreen"), dismissButton: .default(Text("Got it!")))
        }
    }
}

答案 8 :(得分:0)

除了@thisIsTheFoxe的答案,您还可以实现一个简单的扩展名:

扩展名

public extension View {
    func alert(isPresented: Binding<Bool>,
               title: String,
               message: String? = nil,
               dismissButton: Alert.Button? = nil) -> some View {

        alert(isPresented: isPresented) {
            Alert(title: Text(title),
                  message: {
                    if let message = message { return Text(message) }
                    else { return nil } }(),
                  dismissButton: dismissButton)
        }
    }
}

用法:

因此,您现在可以像这样轻松地使用它:

struct ContentView: View {
    @State var showsAlert = false
    var body: some View {
        Button("Show Alert") {
            self.showsAlert.toggle()
        }
        .alert(isPresented: $showsAlert, title: "title", message: "Message") // <- Here
    }
}