如何在数组计数结束时转到新的 UIViewController
How to segue to a new UIViewController at the end of an array count
我先解释一下这个概念。我问用户 3 个问题,这些问题存储在 NSArray
中。有一个UITextField
,用户将用它来写出所有 3 个问题的答案。
我希望发生的情况是,当数组中显示所有三个问题时,下次用户按下下一步 UIButton
时,它会转到一个新的 UIViewController
。我在 main.storyboard 中创建了 segue 并为其指定了一个标识符 countdownSegue。
当我 运行 我的应用程序,当我到达最后一个问题并按下下一步按钮时,应用程序崩溃并且错误是 "fatal error: Array index out of range" - 我不明白为什么。我的代码在下面显示了我的问题数组和我的按钮在按下时发生了什么。非常感谢任何帮助。
let questions = ["Where are you going?", "Which city?", "When do you go?"]
var currentQuestionIndex = 0
let placeholder = ["Country", "City", "Date"]
var currentPlaceholderIndex = 0
@IBAction func nextButton(sender: AnyObject) {
// Initial setup on button press
questionTextField.hidden = false
barImage.hidden = false
questionTextField.placeholder = placeholder[currentPlaceholderIndex]
questionLabel.text = questions[currentQuestionIndex]
questionTextField.resignFirstResponder()
// Reset text field to have no text
questionTextField.text = ""
// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex < questions.count && currentPlaceholderIndex < placeholder.count {
currentQuestionIndex++
currentPlaceholderIndex++
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
} else if currentQuestionIndex > questions.count && currentPlaceholderIndex > placeholder.count {
performSegueWithIdentifier("countdownSegue", sender: self)
}
}
谢谢!
您应该对照 count - 1
检查您的 index
,因为索引是从零开始的。
if currentQuestionIndex < questions.count - 1 && currentPlaceholderIndex < placeholder.count - 1{
currentQuestionIndex++
currentPlaceholderIndex++
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
} else {
performSegueWithIdentifier("countdownSegue", sender: self)
}
编辑:
您在从数组中获取值以显示文本时没有检查索引。当您在最后一个问题上点击 NEXT 按钮时,它会溢出。
@IBAction func nextButton(sender: AnyObject) {
// Initial setup on button press
questionTextField.hidden = false
barImage.hidden = false
questionTextField.resignFirstResponder()
// Reset text field to have no text
questionTextField.text = ""
// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex < questions.count && currentPlaceholderIndex < placeholder.count {
questionTextField.placeholder = placeholder[currentPlaceholderIndex]
questionLabel.text = questions[currentQuestionIndex]
currentQuestionIndex++
currentPlaceholderIndex++
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
} else {
performSegueWithIdentifier("countdownSegue", sender: self)
}
}
我先解释一下这个概念。我问用户 3 个问题,这些问题存储在 NSArray
中。有一个UITextField
,用户将用它来写出所有 3 个问题的答案。
我希望发生的情况是,当数组中显示所有三个问题时,下次用户按下下一步 UIButton
时,它会转到一个新的 UIViewController
。我在 main.storyboard 中创建了 segue 并为其指定了一个标识符 countdownSegue。
当我 运行 我的应用程序,当我到达最后一个问题并按下下一步按钮时,应用程序崩溃并且错误是 "fatal error: Array index out of range" - 我不明白为什么。我的代码在下面显示了我的问题数组和我的按钮在按下时发生了什么。非常感谢任何帮助。
let questions = ["Where are you going?", "Which city?", "When do you go?"]
var currentQuestionIndex = 0
let placeholder = ["Country", "City", "Date"]
var currentPlaceholderIndex = 0
@IBAction func nextButton(sender: AnyObject) {
// Initial setup on button press
questionTextField.hidden = false
barImage.hidden = false
questionTextField.placeholder = placeholder[currentPlaceholderIndex]
questionLabel.text = questions[currentQuestionIndex]
questionTextField.resignFirstResponder()
// Reset text field to have no text
questionTextField.text = ""
// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex < questions.count && currentPlaceholderIndex < placeholder.count {
currentQuestionIndex++
currentPlaceholderIndex++
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
} else if currentQuestionIndex > questions.count && currentPlaceholderIndex > placeholder.count {
performSegueWithIdentifier("countdownSegue", sender: self)
}
}
谢谢!
您应该对照 count - 1
检查您的 index
,因为索引是从零开始的。
if currentQuestionIndex < questions.count - 1 && currentPlaceholderIndex < placeholder.count - 1{
currentQuestionIndex++
currentPlaceholderIndex++
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
} else {
performSegueWithIdentifier("countdownSegue", sender: self)
}
编辑:
您在从数组中获取值以显示文本时没有检查索引。当您在最后一个问题上点击 NEXT 按钮时,它会溢出。
@IBAction func nextButton(sender: AnyObject) {
// Initial setup on button press
questionTextField.hidden = false
barImage.hidden = false
questionTextField.resignFirstResponder()
// Reset text field to have no text
questionTextField.text = ""
// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex < questions.count && currentPlaceholderIndex < placeholder.count {
questionTextField.placeholder = placeholder[currentPlaceholderIndex]
questionLabel.text = questions[currentQuestionIndex]
currentQuestionIndex++
currentPlaceholderIndex++
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
} else {
performSegueWithIdentifier("countdownSegue", sender: self)
}
}