networking - SwiftUI 如何在警报消息中返回 nil
问题描述
在通过导入网络检查网络状态时,我正在努力删除警报中的成功消息。
.onAppear{
viewModel.updateData(baseCode: viewModel.baseCode)
if monitor.isConnected == false {
print("Wifi is connected")
} else if monitor.isConnected == true {
self.showingAlert = true
}
}.alert(isPresented: $showingAlert, content: {
if monitor.isConnected == false {
return Alert(title: Text("Connect to Internet"), message: Text("Kindly Connect to internet to get the latest rates and use the application with accurate calculations"), dismissButton: .default(Text("Okay")))
} else {
return Alert(title: Text("Change Rate Updated"), message: Text("You have successfully updated the change rates."), dismissButton: nil)
}
})
如果一切正常并且我连接到 Wi-fi 而不是
return Alert(title: Text("Change Rate Updated"), message: Text("You have successfully updated the change rates."), dismissButton: nil)
我试图不返回任何东西。因此,如果成功连接到 Internet,则不会出现该消息。
解决方案
Alert
仅在showingAlert
为真时出现。因此,您不需要使用if-statement
in.alert
.alert(isPresented: $showingAlert, content: {
return Alert(title: Text("Connect to Internet"), message: Text("Kindly Connect to internet to get the latest rates and use the application with accurate calculations"), dismissButton: .default(Text("Okay")))
})
测试这段代码
struct OrderCompleteAlert: View {
@State private var isPresented = false
var body: some View {
Button("Show Alert", action: {
isPresented = true
})
.alert(isPresented: $isPresented) {
Alert(title: Text("Order Complete"),
message: Text("Thank you for shopping with us."),
dismissButton: .default(Text("OK")))
}
}
}
推荐阅读
- java - RxJava Android 导致 ConcurrentModificationException
- php - 根据 Laravel 中的 ID 数组对集合进行排序
- javascript - 一个带时间的回调函数 javascript
- css - Github.io 图片链接
- android - Retrofit2 Android:需要一个字符串,但在第 1 行第 12 列路径 $.result 处是 BEGIN_OBJECT
- android - Flutter中映射字符串动态访问var
- coldfusion - 从表中插入表中选择,其中列名 = xxx 和列名2 = yyy 循环太多次
- c++ - 产生不正确 ppm 文件的简单光栅化器
- reactjs - (ESLint)在 VS 2017 反应项目中找不到模块“eslint-config-react-app”
- objective-c - Objective C,使用带有按钮文本的 if 语句