【发布时间】:2021-07-10 12:06:13
【问题描述】:
我是 iOS 新手。我正在尝试做一些测试 UI 以了解这里的一些基本内容,我的问题是 - 我的屏幕上有两个按钮
...
@State private var isAlertVisible: Bool = false
...
Button("1") {
isAlertVisible = true
}
.alert(isPresented: $isAlertVisible, content: {
return Alert(title: Text("Hello there!"), message: Text("This is my first pop-up"), dismissButton: .default(Text("Awesome!")))
})
Button("2") {
}
.alert(isPresented: $isAlertVisible, content: {
return Alert(title: Text("Who's There?"), message: Text("Little old lady."), dismissButton: .default(Text("Little old lady who?")))
})
...
测试场景是:
- 点击 btn 2 - 没有任何反应
- 单击 btn 1 - 打开弹出窗口,
isAlertVisible值现在为==true - 再次单击 btn 2 - 打开弹出窗口,
isAlertVisible值现在是==true
但实际结果是点击按钮 2 被忽略,在调试中我看到 isAlertVisible 的值仍然是 false。
问题是——为什么点击isAlertVisible的btn 1状态后没有变为true?
UPD
我想提出这样的问题
... @State private var isAlertVisible: Bool = false
...
Button("1") {
isAlertVisible = true
}
.alert(isPresented: $isAlertVisible, content: {
return Alert(title: Text("Hello there!"), message: Text("This is my first pop-up"), dismissButton: .default(Text("Awesome!")))
})
假设我有一个 btn 并且第一次点击它,所以我设置了调试(为了查看值),我希望在我第一次点击之后 isAlertVisible 是真的,但是当我第二次点击单击调试器我看到该值仍然是错误的。看起来好像是在单击后将值从true返回到默认状态为false......这可能吗?
【问题讨论】:
-
你需要变量。一个用于第一个警报,一个用于第二个,每个按钮触发不同的警报。按钮 2 现在有一个空操作。他们可能正在共享一个像列表行一样的容器,这就是它第二次起作用的原因。
-
@loremipsum 更新了我的问题
-
@RajaKishan 更新了我的问题
-
没有Minimal Reproducible Example 就无法帮助您解决问题。