如何确定 Xcode 中最后一个激活的 UITextField
How to determine the last activ UITextField in Xcode
我是 swift 的新手,我正在玩一个小转换器应用程序。我有两个 TextFields 和一个 "convert" 按钮。
我想用最后一个活动文本字段的转换值覆盖非活动文本字段。
我知道使用更多文本字段和更多按钮会更容易,但我想以这种方式解决它。
有人可以给我建议吗?
var NumberC = Int(InputCelcius.text!)!
var NumberF = Int(InputFahrenheit.text!)!
if InputCelcius.isFirstResponder {
if (InputCelcius.text?.isEmpty)! {
NumberC=0
print("NumberC is empty")
}
NumberC = NumberC * 9/5 + 32
InputFahrenheit.text = " \(NumberC)"
}
else if InputFahrenheit.isFirstResponder {
if (InputFahrenheit.text?.isEmpty)! {
NumberF=0
print("NumberF is empty")
}
NumberF = (NumberF - 32) * 5/9
InputCelcius.text = " \(NumberF)"
}
我不太确定,但我想如果你按下按钮,按钮本身将成为第一响应者,所以你的代码将无法工作。
正确的方法是设置 IBAction 函数,当您完成对其中一个文本字段的编辑时调用该函数。只需将情节提要中的文本字段连接到代码,就像您使用按钮所做的那样。
然后你可以设置两个函数,每个文本域一个,这样你就不用担心第一响应者了。
我建议使用 UITextFieldDelegate
函数 textFieldDidEndEditing
。确保将 viewController 子类化为 UITextFieldDelegate
.
class ViewController: UIViewController, UITextFieldDelegate {
在 viewDidLoad
中将每个 textField 的委托设置为自身。
override func viewDidLoad() {
super.viewDidLoad()
InputFahrenheit.delegate = self
InputCelsius.delegate = self
}
使用这种方法,您甚至不需要 UIButton,但您可以使用按钮强制 textField 结束编辑,就像这样。
@objc func buttonAction (sender: UIButton) {
self.view.endEditing(true)
}
如果用户正在编辑文本字段,它将调用此委托方法。
func textFieldDidEndEditing (textField: UITextField) {
if textField.isEmpty {return} //this prevents nil errors and user would have to type in "0" if wishing to convert zero.
if textField == InputCelsius {
let conversion = Float(textField.text!) * 9/5 + 32
InputFahrenheit.text = String(format: “%.f”, conversion) //“%.1f” will show one decimal place and so forth
} else if textField == InputFahrenheit {
let conversion = ( Float(textField.text!) - 32 ) * 5/9
InputCelsius.text = String(format: “%.f”, conversion)
}
}
我是 swift 的新手,我正在玩一个小转换器应用程序。我有两个 TextFields 和一个 "convert" 按钮。 我想用最后一个活动文本字段的转换值覆盖非活动文本字段。 我知道使用更多文本字段和更多按钮会更容易,但我想以这种方式解决它。
有人可以给我建议吗?
var NumberC = Int(InputCelcius.text!)!
var NumberF = Int(InputFahrenheit.text!)!
if InputCelcius.isFirstResponder {
if (InputCelcius.text?.isEmpty)! {
NumberC=0
print("NumberC is empty")
}
NumberC = NumberC * 9/5 + 32
InputFahrenheit.text = " \(NumberC)"
}
else if InputFahrenheit.isFirstResponder {
if (InputFahrenheit.text?.isEmpty)! {
NumberF=0
print("NumberF is empty")
}
NumberF = (NumberF - 32) * 5/9
InputCelcius.text = " \(NumberF)"
}
我不太确定,但我想如果你按下按钮,按钮本身将成为第一响应者,所以你的代码将无法工作。
正确的方法是设置 IBAction 函数,当您完成对其中一个文本字段的编辑时调用该函数。只需将情节提要中的文本字段连接到代码,就像您使用按钮所做的那样。
然后你可以设置两个函数,每个文本域一个,这样你就不用担心第一响应者了。
我建议使用 UITextFieldDelegate
函数 textFieldDidEndEditing
。确保将 viewController 子类化为 UITextFieldDelegate
.
class ViewController: UIViewController, UITextFieldDelegate {
在 viewDidLoad
中将每个 textField 的委托设置为自身。
override func viewDidLoad() {
super.viewDidLoad()
InputFahrenheit.delegate = self
InputCelsius.delegate = self
}
使用这种方法,您甚至不需要 UIButton,但您可以使用按钮强制 textField 结束编辑,就像这样。
@objc func buttonAction (sender: UIButton) {
self.view.endEditing(true)
}
如果用户正在编辑文本字段,它将调用此委托方法。
func textFieldDidEndEditing (textField: UITextField) {
if textField.isEmpty {return} //this prevents nil errors and user would have to type in "0" if wishing to convert zero.
if textField == InputCelsius {
let conversion = Float(textField.text!) * 9/5 + 32
InputFahrenheit.text = String(format: “%.f”, conversion) //“%.1f” will show one decimal place and so forth
} else if textField == InputFahrenheit {
let conversion = ( Float(textField.text!) - 32 ) * 5/9
InputCelsius.text = String(format: “%.f”, conversion)
}
}