【问题标题】:How to call textFieldShouldReturn from a number pad Xcode 9 Swift 4如何从数字键盘 Xcode 9 Swift 4 调用 textFieldShouldReturn
【发布时间】:2018-04-15 10:35:44
【问题描述】:

我正在尝试在我的应用中设置本地通知。到目前为止,它可以工作,尽管它只适用于默认键盘。用户为通知输入的值只能是数字。问题是数字键盘不包含用户需要按下以发送通知的完成按钮;我正在使用 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)
    }
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    当您的键盘类型为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
    }
    

    【讨论】:

    • @matt 好的,我改了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2018-03-06
    • 2018-03-10
    相关资源
    最近更新 更多