2017-06-19 75 views
0

我有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實際上不應該導致崩潰。 –

回答

0

發生錯誤是因爲目標/操作方法必須在類的頂層聲明。

你大概的意思

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) 

} 
0

看起來好像你有一個從你的代碼中刪除的故事板上的懸掛連接,但是在故事板中卻忘了這麼做。右鍵點擊視圖(可能是login按鈕)並檢查是否有死連接。

0

我認爲這將是對你有用(您可以更改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") 

    } 
0

更換

func registerButtonTwo(_ sender: Any) { 
      self.performSegue(withIdentifier: "registertologin", sender: self) 
     } 

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