我有一段代碼,旨在讓用戶輸入他們的名字,並按下按鈕時前進到下一個屏幕。如果沒有值輸入,程序不應允許用戶繼續。程序會檢測何時不輸入名稱,也不會移動到下一個屏幕,而是終止於類型爲NSException的未捕獲異常。我希望允許用戶繼續輸入值(名稱),直到實際輸入名稱(即名稱值不爲空)。有什麼辦法可以做到這一點? while/for循環似乎不起作用。計數字符的UITextField崩潰與NSException
@IBAction func NameEditingDidChange(_ sender: Any) {
NextButton.isEnabled = true
}
// Once button is pressed
@IBAction func NextButtonPressed(_ sender: Any) {
// Gets user's name from textfield
print("12345abcde")
self.name = NameInput.text!
// If textfield is not empty, moves to next storyboard
if name.characters.count != 0 {
print("in ")
performSegue(withIdentifier: "fistBump", sender: nil)
}
else {
// Prints this line and terminates
print("text field is empty")
}
需要'self.'在'self.name.characters.count'我想。 – Eggsalad