我有4个文本字段,其中2个是密码和确认密码。在进入登录屏幕之前,我需要检查它们是否相同或其中包含文本。这是我的代码:
func registerButtonTapped() {
var a = false
var b = false
if passwordFieldR.text! == confirmFieldR.text! {
a = true
} else {
nonMatchingPasswords.text = "Passwords Do Not Match"
}
if(passwordFieldR.text! == "" || confirmFieldR.text! == "") {
nonMatchingPasswords.text = "Password Field is empty"
} else {
b = true
}
if a == true && b == true {
func registerButtonTwo(_ sender: Any) {
self.performSegue(withIdentifier: "registertologin", sender: self)
}
}
}
应用程序崩溃并在控制台中向我显示以下错误消息:
2017-06-19 21:29:20.926579+1200 Altitude[1655:588172] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-06-19 21:29:20.927965+1200 Altitude[1655:588172] [MC] Reading from public effective user settings.
2017-06-19 21:29:30.784007+1200 Altitude[1655:588172] -[Altitude.secondscreenviewcontroller registerButtonTwo:]: unrecognized selector sent to instance 0x100d085d0
2017-06-19 21:29:30.784671+1200 Altitude[1655:588172] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Altitude.secondscreenviewcontroller registerButtonTwo:]: unrecognized selector sent to instance 0x100d085d0'
*** First throw call stack:
(0x18add2fd8 0x189834538 0x18add9ef4 0x18add6f4c 0x18acd2d2c 0x190f370ec 0x190f3706c 0x190f215e0 0x190f36950 0x190f3646c 0x190f31804 0x190f02418 0x1916fbf64 0x1916f66c0 0x1916f6aec 0x18ad81424 0x18ad80d94 0x18ad7e9a0 0x18acaed94 0x18c718074 0x190f67130 0x1000e075c 0x189cbd59c)
libc++abi.dylib: terminating with uncaught exception of type NSException
发生了什么事以及如何解决这个问题?
我认为这对你有用(你可以将5个数字更改为任何你想要的密码字符的最少数字)
if ((passwordFieldR.text?.characters.count)! > 5) {
if passwordFieldR.text! == confirmFieldR.text! {
print("registertologin")
} else {
print("Password Does Not Match Confirm Password")
}
} else {
print("password Characters should be more Than 5")
}
发生错误是因为目标/操作方法必须在类的顶层声明。
你可能是说
func registerButtonTapped() {
var a = false
var b = false
if passwordFieldR.text! == confirmFieldR.text! {
a = true
} else {
nonMatchingPasswords.text = "Passwords Do Not Match"
}
if(passwordFieldR.text! == "" || confirmFieldR.text! == "") {
nonMatchingPasswords.text = "Password Field is empty"
} else {
b = true
}
if a == true && b == true {
registerButtonTwo(self)
}
}
func registerButtonTwo(_ sender: Any) {
self.performSegue(withIdentifier: "registertologin", sender: self)
}
PS:
registerButtonTapped()
可以写得更高效
func registerButtonTapped() {
guard let password = passwordFieldR.text, !password.isEmpty,
let confirm = confirmFieldR.text, !confirm.isEmpty else {
nonMatchingPasswords.text = "Password Field is empty"
return
}
guard password == confirm else {
nonMatchingPasswords.text = "Passwords Do Not Match"
return
}
registerButtonTwo(self)
}
您似乎有一个来自故事板的悬空连接,您从代码中删除了该连接,但忘记在故事板中这样做。右键单击视图(可能是
login
按钮)并检查是否存在死连接。
更换
func registerButtonTwo(_ sender: Any) {
self.performSegue(withIdentifier: "registertologin", sender: self)
}
仅
self.performSegue(withIdentifier: "registertologin", sender: self)
验证密码并确认密码
func isValidPassword(passworld: String, confirmPassworld: String) -> Bool{
if confirmPassworld.lowercased() == passworld.lowercased(){
return true
}
return false
}