英文:
SwiftUI - only one alert showing in nested if
问题
我正在创建一个注册视图,并且有两个警报要显示。一个是在电子邮件或密码不符合要求时显示的,另一个是在电子邮件已经保存在系统中时使用的。问题是,现在只显示检查电子邮件和密码是否符合要求的警报。
Button("注册") {
toggle.toggle()
if (isValidEmailAddr(strToValidate: userCredentials.email) && isValidPassword(strToVailidate: userCredentials.password)) {
if !emailDoesExist(emailToCheck: userCredentials.email) {
print("电子邮件和密码有效")
saveJSON(email: userCredentials.email, password: userCredentials.password, nickname: userCredentials.nickname)
} else {
print("电子邮件地址已经存在于系统中")
self.showAlert1.toggle()
}
}
else {
self.showAlert0.toggle()
}
}
.alert(isPresented: $showAlert1, content: { self.alert1 })
.alert(isPresented: $showAlert0, content: { self.alert0 })
.font(.title)
.buttonStyle(.bordered)
.frame(width:200, height: 100)
英文:
So I am creating a register view and I have two alerts to show. One is showing if the email or the password don't meet the requirements and the other one is used if the email is already saved in the system. The issue is that the single alert showing up is the one that checks if the email and password meet that set of requirements.
Button ("Sign Up") {
toggle.toggle()
if (isValidEmailAddr(strToValidate: userCredentials.email) && isValidPassword(strToVailidate: userCredentials.password)) {
if !emailDoesExist(emailToCheck: userCredentials.email) {
print("Email and Passowrd are valid")
saveJSON(email: userCredentials.email, password: userCredentials.password, nickname: userCredentials.nickname)
} else {
print("email address already in the system")
self.showAlert1.toggle()
}
}
else {
self.showAlert0.toggle()
}
}
.alert(isPresented: $showAlert1, content: { self.alert1 })
.alert(isPresented: $showAlert0, content: { self.alert0 })
.font(.title)
.buttonStyle(.bordered)
.frame(width:200, height: 100)
答案1
得分: 1
问题在于您无法将超过一个警报附加到同一视图,而现在您的按钮有两个。
有几种方法可以解决这个问题,以获取只有一个警报,这里我使用了一种简单的方法,其中我使用了一个第二属性,该属性保存了一个错误消息,可以在警报中使用。
@State private var showAlert = false
@State private var errorMessage = ""
然后,您需要调整实际的.alert
修饰符,但由于我不知道self.alert0/1
是什么,您需要自己完成(很可能只保留其中一个,并利用errorMessage
)。
英文:
The problem is that you cannot attach more than one alert to the same view and now you have two for your button.
There are several ways to approach this to get only one alert and here I use a simple one where I use a second property that holds an error message that can be used in the alert
@State private var showAlert = false
@State private var errorMessage = ""
if (isValidEmailAddr(strToValidate: userCredentials.email) && isValidPassword(strToVailidate: userCredentials.password)) {
if !emailDoesExist(emailToCheck: userCredentials.email) {
saveJSON(email: userCredentials.email, password: userCredentials.password, nickname: userCredentials.nickname)
} else {
errorMessage = "Mail already exists"
self.showAlert.toggle()
}
} else {
errorMessage = "Mail or password is incorrect"
self.showAlert.toggle()
}
Then you would need to adjust the actual .alert modifier but since I don't know what self.alert0/1 are you need to do that yourself (most likely by keeping only one of them and make use of errorMessage
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论