【问题标题】:Recieving build error when creating an alert in SwiftUI在 SwiftUI 中创建警报时收到构建错误
【发布时间】:2020-04-18 03:40:57
【问题描述】:

我不知道这些是否来自 SwiftUI 太新以至于我做错了什么。我只导入 SwiftUI。

Tryng to declare an alert in SwiftUI but keeps giving me error on build.

All the errors

I am getting the option for a UIAlertController, not the standard

【问题讨论】:

  • 不要贴代码图片,直接粘贴代码。欢迎来到stakeoverflow,@Luke Kasson..!!
  • 感谢您告诉我!

标签: swift xcode swiftui


【解决方案1】:

如果你有一个primaryButton,你需要提供一个secondaryButton。此处的代码有效,应该可以回答您的问题:

import SwiftUI

struct ContentView: View {

@State var showsAlert = false
@State var errorMsg = "xxxx"

var body: some View {
    Button(action: { self.showsAlert = true }) {
        Text("press to test").padding(10).border(Color.black)
    }.alert(isPresented: self.$showsAlert) {
        Alert(title: Text(self.errorMsg),
              message: Text("text message"),
              primaryButton: Alert.Button.default(Text("yes"), action: {
                print("---> yes")
              }),
              secondaryButton: Alert.Button.default(Text("no"), action: {
                print("---> no")
        }))
    }
}
}

【讨论】:

  • 在输入“Alert(”时,我什至没有选择主按钮或辅助按钮。
  • 如果您复制并粘贴此代码,它对您不起作用?
【解决方案2】:

由于某种原因,它无法识别 SwiftUI 警报,而只能识别 UIAlertContrller。必须声明 SwiftUI.Alert() 并且这有效。

.alert(isPresented: $showAlert) {
                   SwiftUI.Alert(title: Text("Hello"), message: Text("Good bye"), dismissButton: .cancel({
                       print("Canclel tapped")
                   }))
               }

【讨论】:

  • 有些东西不在这里,你不应该这样做。你导入了 SwiftUI 吗?您能否向我们展示您使用的完整代码,以便我们追踪问题。
  • 无论视图是什么,空的,只是一个按钮等,它都会给出相同的错误。我只导入了 SwiftUI。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
相关资源
最近更新 更多