gpt4 book ai didi

ios - 如何使 UITextField 的行为类似于 swift 3 中的银行输入格式?

转载 作者:行者123 更新时间:2023-11-30 12:51:04 25 4
gpt4 key购买 nike

我正在尝试制作一个类似于银行应用程序中使用的文本字段,当您输入金额时,它以 0.00 美元开头,如果我输入 5,它会显示 0.05 美元,如果我输入输入 4,它将显示 $0.54,依此类推。因此,只要您继续输入,它就会从右边开始填充每个数字。我知道我需要使用shouldChangeCharactersInRange,但我的TextField位于alertController中,所以这让事情变得有点困难,我不知道如何处理它背后的逻辑。这是我到目前为止所得到的

@IBAction func myMethod(_ sender: Any) {

let myAlert = UIAlertController(title: "Some words", message: "\n", preferredStyle: .alert)

myAlert.addTextField(configurationHandler: { (textField) -> Void in
textField.placeholder = "TextField1"

})

myAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

myAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in

//some action
}))

self.present(myAlert, animated: true, completion: nil)

}

希望我已经足够彻底地解释了一切。提前感谢您的帮助!

最佳答案

看起来缺少的只是设置该 UITextField 的委托(delegate)。在configurationHandler中设置委托(delegate)应该足以纠正这个问题。

如果您的 shouldChangeCharactersInRange 函数位于父文件中,请将其设置为委托(delegate)。否则,使用所需的方法创建 UITextFieldDelegate 类,然后将其实例设置为委托(delegate)。

class MyTextFieldDelegate: UITextFieldDelegate {
// Implement shouldChangeCharactersInRange and other delegates here
}
...
// View Controller presenting the alert
var textFieldDelegate: MyTextFieldDelegate
override func viewDidLoad() {
// Other stuff here

// Set your delegate
textFieldDelegate = MyTextFieldDelegate()
}

...

@IBAction func myMethod(_ sender: Any) {

let myAlert = UIAlertController(title: "Some words", message: "\n", preferredStyle: .alert)

myAlert.addTextField(configurationHandler: { (textField) -> Void in
textField.placeholder = "TextField1"
textField.delegate = self.textFieldDelegate

})

myAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

myAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in

//some action
}))

self.present(myAlert, animated: true, completion: nil)
}

关于ios - 如何使 UITextField 的行为类似于 swift 3 中的银行输入格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com