我正在尝试在我的应用中设置本地通知。到目前为止它可以工作,尽管它只适用于默认键盘。用户为通知输入的值只能是数字。问题是数字键盘不包含用户需要按下才能发送通知的完成按钮;我正在使用 textFieldShouldReturn 函数。我有一个以编程方式添加的完成按钮,但我的问题是:
如何调用我在 numberPad 中创建的按钮内的 textFieldShouldReturn 函数?
如果不能这样做,我是否需要完全更改我的通知?
我有七个开关和文本字段,但出于演示目的我只显示一个:
import UIKit
import UserNotifications
class SettingsViewController: UIViewController, UITextFieldDelegate {
let SCV = SettingsViewController()
@IBOutlet weak var field1: UITextField!
@IBOutlet weak var switch1: UISwitch!
@IBOutlet weak var cell1: UIView!
let center = UNUserNotificationCenter.current()
override func viewDidLoad() {
super.viewDidLoad()
field1 = self
field1 = "0"
field1 = UIColor.white
if let ebrSwitchState = UserDefaults.standard.value(forKey: "ebrState") as? Bool{
if switch1state == true{
switch1(true, animated: true)
if let text1 = UserDefaults.standard.value(forKey: "ebrValue") as? String{
field1 = true;
field1 = ebrText
}
}
else{
switch1.setOn(false, animated: false)
field1.isEnabled = false
}
}
}
@IBAction func ebrValueChanged(_ sender: UISwitch) {
if ebrSwitch.isOn{
field1.isEnabled = true
UserDefaults.standard.set(true, forKey: "state1")
}
else{
field1.isEnabled = false
field1.text = "0"
UserDefaults.standard.set(false, forKey: "state1")
center.removePendingNotificationRequests(withIdentifiers: ["1"])
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
if (textField.tag == 0){
UserDefaults.standard.set(ebrField.text, forKey: "1Value")
LocalPushManager.shared.sendLocalPush(in: 1800, plant: 0)}
else if(textField.tag == 1){
UserDefaults.standard.set(etrField.text, forKey: "2Value")
LocalPushManager.shared.sendLocalPush(in: 1800, plant: 1)
}
else if(textField.tag == 2){
UserDefaults.standard.set(fscField.text, forKey: "fscValue")
LocalPushManager.shared.sendLocalPush(in: 1800, plant: 2)
}
else if(textField.tag == 3){
UserDefaults.standard.set(jdpField.text, forKey: "jdpValue")
LocalPushManager.shared.sendLocalPush(in: 1800, plant: 3)
}
else if(textField.tag == 4){
UserDefaults.standard.set(phrField.text, forKey: "phrValue")
LocalPushManager.shared.sendLocalPush(in: 1800, plant: 4)
}
else if(textField.tag == 5){
UserDefaults.standard.set(tcrField.text, forKey: "tcrValue")
LocalPushManager.shared.sendLocalPush(in: 1800, plant: 5)
}
else if(textField.tag == 7){
UserDefaults.standard.set(mlsField.text, forKey: "mlsValue")
LocalPushManager.shared.sendLocalPush(in: 1800, plant: 6)
}
else{print("text field area went wrong")}
return true;
}
// Hide keyboard when user touches outside keyboard
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
最佳答案
当您的键盘类型为NumberPad时无法调用
尝试改用它,它可以节省您的代码:)
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("Can't be reach")
return true
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == yourNumberPadTextFiled {
print("\(String(describing: textField.text))")
/// listen the string change and do whether you want
}
return true
}
关于ios - 如何从数字键盘 Xcode 9 Swift 4 调用 textFieldShouldReturn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577760/