我正在尝试在我的应用中设置本地通知。到目前为止它可以工作,虽然它只适用于默认键盘。用户为通知输入的值应仅为数字。问题是数字键盘不包含用户需要按下以发送通知的完成按钮;我正在使用textFieldShouldReturn函数。我有一个以编程方式添加的完成按钮,但我的问题是:
如何在我在numberPad中创建的按钮内调用textFieldShouldReturn func?
如果无法执行此操作,是否需要完全更改通知?
我有七个开关和文本字段,但我只会出示一个用于演示目的:
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)
}
}
答案 0 :(得分:0)
当您的键盘类型为 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
}