有3个文本字段。默认情况下,程序启动后将选择第一个。有一个下一个按钮,通过按该按钮可以选择下一个文本字段。由于某种原因,它崩溃了。按下按钮时崩溃。有人可以解释我在做什么错
override func viewDidLoad() {
super.viewDidLoad()
//select default
textfield.becomeFirstResponder()
}
//should go to next textfield when pressed
@IBAction func NextButton()
{
// if textfield is selected
if(textfield.becomeFirstResponder() == true)
{
textfield.resignFirstResponder()
textfieldTwo.becomeFirstResponder()
}
if(textfieldTwo.becomeFirstResponder() == true)
{
textfieldTwo.resignFirstResponder()
textfieldThree.becomeFirstResponder()
}
}
答案 0 :(得分:0)
不要使用becomeFirstResponder()
来检查文本字段是否处于活动状态,而应使用isFirstResponder()
。还要注意,我为else if
添加了textfieldTwo
。
@IBAction func NextButton()
{
// if textfield is selected
if(textfield.isFirstResponder() == true)
{
textfield.resignFirstResponder()
textfieldTwo.becomeFirstResponder()
}
else if(textfieldTwo.isFirstResponder() == true)
{
textfieldTwo.resignFirstResponder()
textfieldThree.becomeFirstResponder()
}
}