【问题标题】:SwiftUI - show an alertSwiftUI - 显示警报
【发布时间】:2021-08-11 15:20:43
【问题描述】:

我希望在用户无需任何操作而快速移动到新页面时显示警报,我使用 NavigationLink

ContentView.swift

struct ContentView: View {
    var body: some View {
        VStack{
            NavigationLink(destination: SecondView()){
                Text("Go to second view")
            }
        }
    }
}

SecondView.swift

struct SecondView: View {
    @State var showAlert = true
    
    var body: some View {
        // i want to show alert when navigate to this view
        VStack{
            Text("Second View")
                .alert(isPresented: $showAlert) {
                    Alert(title: Text("You are in second view"))
                }
        }
    }
}

你能帮我吗?

【问题讨论】:

    标签: ios swift xcode swiftui


    【解决方案1】:

    VStack 出现时将showAlert 的值更改为true,就像那样

    struct SecondView: View {
        @State var showAlert = false 
        
        var body: some View {
            // i want to show alert when navigate to this view
            VStack{
                Text("Second View")
                    .alert(isPresented: $showAlert) {
                        Alert(title: Text("You are in second view"))
                    }
            }.onAppear{
              showAlert = true
            }
        }
    }
    

    【讨论】:

      【解决方案2】:
      // MARK:- CUSTOM ALERT CLASS
      
      import SwiftUI
      
      private struct AlertView: View {
          
          let loaderBackgroundColor: Color = .secondary
          let loaderCornerRadius: CGFloat =  10.0
          
          /// parameter to hide and show loader
          @Binding var isShowing: Bool
          
          let titleText: String
          let messageText: String
          let buttonText: String
          
          var body: some View {
              
              ZStack(alignment: .center) {
                  
                  VStack(spacing: 10) {
                      
                      if titleText.count > 0 {
                          Text(titleText)
                              .foregroundColor(.black)
                              .fontWeight(.bold)
                              .padding(.bottom, 10)
                      }
                      
                      Text(messageText)
                          .font(.system(size: 14))
                          .foregroundColor(.gray)
                      
                      Spacer()
                      
                      Button(action: {
                          APAlert.shared.remove()
                      }) {
                          Text(buttonText)
                              .font(.system(size: 14))
                              .foregroundColor(.white)
                              .fontWeight(.bold)
                      }
                      .frame(width: 100, height: 40)
                      .background(Color.black)
                      .cornerRadius(20.0)
                  }
                  .padding(EdgeInsets(top: 40, leading: 20, bottom: 30, trailing: 20))
                  .frame(width: 300, height: 200)
                  .background(Color.white)
                  .cornerRadius(10.0)
                  .shadow(color: Color(.sRGBLinear, white: 0, opacity: 0.13), radius: 10.0)
              }
          }
      }
      
      public class APAlert {
          public static var shared = APAlert()
          private init() { }
          private var popupWindow: AlertWindow?
          
          public func showAlert(title: String = "Error", message: String = "Request could not be processed due to a server error. The request may succeed if you try again.", buttonTitle: String = "Ok") {
              setAlertBody(title: title, message: message, buttonTitle: buttonTitle)
          }
          
          /// function to remove loader from screen.
          public func remove() {
              removeAlert()
          }
          
      }
      
      // MARK: - AlertWindow
      private class AlertWindow: UIWindow {
      }
      
      private extension APAlert {
          
          func setAlertBody(title: String = "", message: String = "", buttonTitle: String = "") {
              
              let windowScene = UIApplication.shared
                  .connectedScenes
                  .filter { $0.activationState == .foregroundActive }
                  .first
              if let windowScene = windowScene as? UIWindowScene {
                  popupWindow = AlertWindow(windowScene: windowScene)
                  popupWindow?.frame = UIScreen.main.bounds
                  popupWindow?.backgroundColor = .clear
                  popupWindow?.rootViewController = UIHostingController(rootView: AlertView(isShowing: .constant(true), titleText: title, messageText: message, buttonText: buttonTitle))
                  popupWindow?.rootViewController?.view.backgroundColor = UIColor.gray.withAlphaComponent(0.6) //.opacity(0.5)
                  popupWindow?.makeKeyAndVisible()
              }
          }
          /// Remove loader from screen
          func removeAlert() {
              let alertwindows = UIApplication.shared.windows.filter { $0 is AlertWindow }
              alertwindows.forEach { (window) in
                  window.removeFromSuperview()
              }
              popupWindow = nil
          }
      }
      

      现在我们将使用 APAlert 类显示警报

      import SwiftUI
      
      struct ContentView: View {
          
          var body: some View {
              VStack{
                  Text("SHOW ALERT").onTapGesture {
                      APAlert.shared.showAlert(title: "Title", message: "Error", buttonTitle: "OK")
                  }
              }
          }
      }
      

      参考:- https://github.com/Arvindcs/APAlertView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 2020-02-23
        • 1970-01-01
        相关资源
        最近更新 更多