如何使密码快速确认3

问题描述:

我有4个文本字段,其中2个是密码并确认密码。在进入登录屏幕之前,我需要检查它们是否相同或是否有文本。我的继承人代码:如何使密码快速确认3

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 

这是怎么回事,我该如何解决这一问题?

+1

你为什么用另一种方法声明'FUNC registerButtonTwo(...'在哪里你将目标选择器添加到按钮的代码? –

+0

@ defence2000x实际上不应该导致崩溃。 –

发生错误是因为目标/操作方法必须在类的顶层声明。

你大概的意思

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按钮)并检查是否有死连接。

我认为这将是对你有用(您可以更改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 registerButtonTwo(_ sender: Any) { 
      self.performSegue(withIdentifier: "registertologin", sender: self) 
     } 

self.performSegue(withIdentifier: "registertologin", sender: self)