swift - 在 swiftUI 中禁用特定的行/按钮
问题描述
我有这个代码。我只想在名称为“Aditya”时禁用点击,其余的都是可点击的。我目前的试用版不起作用。如何根据某些属性禁用特定的行/按钮。在这种情况下,名称为“Aditya”
import SwiftUI
struct ContentView: View {
var someelements = ["Aditya" , "Kappor" , "Chattarjee", "Mithun"]
var body: some View {
ForEach (self.someelements , id: \.self ) { something in
EditForm(name: something)
//Below is my trial that do not work.
if something == "Aditya" {
self.disabled(true)
}
}
}
}
struct EditForm : View {
var name : String
var body: some View {
Group {
Button (action: {
//some action
}) {
HStack {
Text(self.name)
.scaledToFit()
.foregroundColor(.blue)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方案
struct ContentView: View {
var someelements = ["Kappor" , "Chattarjee", "Mithun", "Aditya"]
var body: some View {
List {
ForEach (self.someelements , id: \.self ) { value in
Button(action: {
print("tapped")
}, label: {
Text("\(value)")
})
.disabled(value == "Aditya") // Disable tap action only for that value here.
}
}
}
}
推荐阅读
- php - 树形菜单 php mysql 列表
- java - 不推荐使用默认构造函数时如何调用父类的构造函数
- typescript - 是否有 ArgumentsType
像 ReturnType 在打字稿中? - sql - 使用 Microsoft.Jet.OLEDB.4.0 连接到 ms sql 2017 服务器
- python - 无法使用 BeautifulSoup 或 golang colly 解析 HTML
- javascript - 修复桌面上 div 的移动视图
- python - 打印关于类中对象的值 (Python)
- java - 通过套接字连接使用 DataOutputStream 发送数据是否安全?
- graphql - 无法在 AEM 中使用 GraphQL
- php - 发送 AUTH LOGIN 命令失败。错误:504 5.7.4 使用 php 发送电子邮件时无法识别的身份验证类型