Swift: มีวิธีเมื่อมีคนแตะปุ่มบนคีย์บอร์ดหรือไม่?

มีวิธีที่ฉันสามารถใช้ได้เมื่อมีการแตะปุ่มใด ๆ บนแป้นพิมพ์บนแป้นพิมพ์ที่ใส่ข้อมูลลงในช่องข้อความใน Swift หรือไม่ ฉันจะทำอย่างไร (เช่น แสดงรูปภาพในมุมมองด้านขวาของ UITextField) เมื่อคีย์ใด ๆ ที่ใส่อินพุตใน UITextField (ยกเว้น เช่น คีย์อย่าง caps lock และปุ่มอิโมจิ / สลับภาษา) ถูกแตะ


person Joe N    schedule 22.02.2019    source แหล่งที่มา
comment
คุณได้ตรวจสอบแล้ว UITextFieldDelegate?   -  person Alexander    schedule 22.02.2019


คำตอบ (6)


คุณสามารถทำได้ง่ายมาก

นี่คือรหัส:

extension ViewController : UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        let userImageView = UIImageView(image: UIImage(named: "user"))
        userImageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        self.txtField.rightViewMode = .always

        if let textFieldValue = textField.text,
            let textFieldRange = Range(range, in: textFieldValue) {
            let newText = textFieldValue.replacingCharacters(in: textFieldRange, with: string)
            if newText == "" {
                textField.rightView = nil
            } else {
                textField.rightView = userImageView
            }
        }
        return true

    }
}

เอาท์พุท:

ป้อนคำอธิบายรูปภาพที่นี่

หากคุณประสบปัญหาใดๆ ในเรื่องนี้ โปรดแจ้งให้เราทราบ

person iOS Team    schedule 22.02.2019
comment
@JoeN คำตอบเดียวกันได้รับ 30 นาทีก่อนคำตอบนี้ ฉันไม่เข้าใจว่าทำไมคุณไม่ยอมรับคำตอบของฉัน??? - person Jarvis The Avenger; 25.02.2019

โดยทั่วไปแป้นพิมพ์จะมาในกรณีของ UITextfield และ UITextView ลองใช้ผู้รับมอบสิทธิ์ที่กล่าวถึงด้านล่าง

ดังนั้น ในกรณีของ UITextField

- (BOOL)textField:(UITextField *)textField
          shouldChangeCharactersInRange:(NSRange)range
          replacementString:(NSString *)string {

    // Do something here...
}

เมื่อคุณใช้แป้นพิมพ์ในกรณีของ UITextView :

- (BOOL)textView:(UITextView *)textView
      shouldChangeTextInRange:(NSRange)range 
      replacementText:(NSString *)text {

    // Do something here...
}

โปรดจำไว้ว่าทุกครั้งที่ผู้ใช้ป้อนอักขระบางตัวหรือกดปุ่มบนแป้นพิมพ์ วิธีนี้จะเรียกและคุณจะรู้ข้อความที่เขาเลือกจากแป้นพิมพ์

person Hairsh Kuramsetty    schedule 22.02.2019
comment
คำถามนี้ติดแท็ก Swift ไม่ใช่ Objective-C - person rmaddy; 22.02.2019
comment
มีวิธีที่คุณเป็นคนอื่นสามารถใส่สิ่งนี้ลงใน Swift ได้หรือไม่? - person Joe N; 22.02.2019
comment
@JoeN ไม่มีอะไรที่นี่ที่คุณไม่สามารถมองเห็นได้เพียงแค่ดูเอกสารอ้างอิงสำหรับ UITextFieldDelegate และ UITextViewDelegate - person rmaddy; 22.02.2019

คุณสามารถใช้ผู้รับมอบสิทธิ์นี้เพื่อควบคุม:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

   // String 键盘上新输入的字符,“”代表删除

    //返回true会添加进去,返回false会忽略掉
    return true
}
person Dandelion    schedule 22.02.2019
comment
โปรดทราบว่าสามารถเรียกสิ่งนี้ได้ในกรณีที่ผู้ใช้ไม่ได้แตะคีย์ เช่น เมื่อผู้ใช้ทำการตัดหรือวาง - person rmaddy; 22.02.2019
comment
ใช่ มันเพียงตอบสนองโค้ดที่จะแทรกลงในการควบคุมของคุณ - person Dandelion; 22.02.2019
comment
จากเงื่อนไขนี้ ฉันคิดว่าเพียงสร้าง inputView แบบกำหนดเอง (มุมมองแป้นพิมพ์) เท่านั้นที่สามารถตอบสนองความต้องการได้ - person Dandelion; 22.02.2019
comment
ครึ่งนี้ได้ผล rightView จะไม่หายไปตอนนี้แม้ว่าฉันจะ backspace ทุกสิ่งที่ฉันพิมพ์ก็ตาม - person Joe N; 22.02.2019

ใช่ คุณสามารถทำได้โดยเพิ่ม addTarget ใน viewDidLoad

override func viewDidLoad() {
    // do your works
    self.yourtextfield.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)
}

@objc func textFieldChanged() {
     // do your work here 
}

addTarget คืออะไร?

เชื่อมโยงวัตถุเป้าหมายและวิธีการดำเนินการกับตัวควบคุม

คุณจดบันทึก addTarget ว่าการกระทำใดที่คุณต้องการ (เช่น การแก้ไขการเปลี่ยนแปลง ฟิลด์ข้อความได้เริ่มแก้ไข หรือสิ้นสุดการแก้ไข) และวิธีการเลือกเชื่อมโยง เมื่อตัวควบคุมถูกเรียก (นี่คือการแก้ไขการเปลี่ยนแปลง) วิธีการเลือกนี้จะถูกเรียกใช้และคุณสามารถจัดการได้ รหัสสิ่งที่คุณต้องการเกี่ยวกับการควบคุม

person Ankur Lahiry    schedule 22.02.2019

คุณสามารถทำได้สองวิธี:

  1. UITextFieldDelegate

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
       // Observer changes in text....
       return true
    }
    
  2. กลไกการกำหนดเป้าหมาย:

ใน viewDidLoad ของคุณให้เพิ่มโค้ดต่อไปนี้:

self.textfield.addTarget(self, action: #selector(textFieldTextChanged), for: .editingChanged)

และวิธีการปฏิบัติ:

@objc func textFieldTextChanged() {
     print(self.textfield.text!)
}

อัปเดต:

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
     let rightView = UIView(frame: CGRect(x: -10, y: 0, width: 20, height: 20))

    override func viewDidLoad() {
        super.viewDidLoad()
        rightView.backgroundColor = .red
        textField.rightView = rightView
        textField.rightViewMode = .always
    }


}

extension ViewController : UITextFieldDelegate {
    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {

        if let text = textField.text, let textRange = Range(range, in: text) {
            let updatedText = text.replacingCharacters(in: textRange, with: string)

            if updatedText == "" {
                textField.rightView = nil
            } else {
                textField.rightView = rightView
            }

        }
        return true
    }

}
person Jarvis The Avenger    schedule 22.02.2019
comment
ตกลง. ผมใช้อันแรก มันเกือบจะใช้งานได้ยกเว้นเมื่อฉันกด Backspace ปุ่มเคลียร์ยังอยู่ ฉันจะทำให้มันหายไปได้อย่างไรเมื่อฉัน backspaced ทุกอย่างออกไปแล้ว? - person Joe N; 22.02.2019
comment
ปุ่มใสอันไหน?? คุณช่วยเพิ่มเข้าไปในคำถามของคุณได้ไหม?? - person Jarvis The Avenger; 22.02.2019
comment
โอ้ ขอโทษที ฉันลืมบอกไป รูปภาพที่แสดงใน rightView ดูเหมือนปุ่มที่ชัดเจน - person Joe N; 22.02.2019
comment
1. ของคุณใช้งานได้ แต่เมื่อฉันกด Backspace และล้างทุกอย่างในช่องข้อความออก ฉันก็อยากจะกำจัด rightView ด้วย - person Joe N; 22.02.2019
comment
@JoeN คุณเพียงแค่ต้องเพิ่มการเช็คอินวิธีการนั้นเมื่อข้อความในช่องข้อความของคุณว่างเปล่า ฉันคิดว่าคุณเพียงแค่ต้องสังเกตลำดับและฉันมั่นใจว่าคุณจะได้มัน - person Jarvis The Avenger; 22.02.2019
comment
@JoeN ชำระเงินคำถามนี้: stackoverflow .com/questions/51894973/ - person Jarvis The Avenger; 22.02.2019
comment
@JoeN หากคุณสามารถรับเหตุการณ์ backspace ได้ มันจะง่ายเพียงตรวจสอบว่าข้อความฟิลด์ข้อความของคุณได้รับเมื่อใด และเพียงลบมุมมองที่ถูกต้องของคุณออก - person Jarvis The Avenger; 22.02.2019
comment
ไม่มีทางแก้ไข?? - person Joe N; 22.02.2019
comment
@JoeN ชำระเงินวิธีแก้ปัญหา - person Jarvis The Avenger; 22.02.2019

โซลูชันของฉันสำหรับปุ่มล้างแบบกำหนดเอง:

เพียงเพิ่มสิ่งนี้นอกชั้นเรียนของคุณ

extension ViewController: UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let clearButton = UIImageView(frame:
        CGRect(x: 7, y: 7, width: 16, height: 16))
    clearButton.image = #imageLiteral(resourceName: "Clear")

    let iconContainerView: UIView = UIView(frame:
        CGRect(x: 0, y: 0, width: 37, height: 30))
    iconContainerView.addSubview(clearButton)
    iconContainerView.tintColor = #colorLiteral(red: 0.5568627451, green: 0.5568627451, blue: 0.5764705882, alpha: 1)

    textField.rightView = iconContainerView
    textField.rightViewMode = .whileEditing

    if let textFieldValue = textField.text, let textFieldRange = Range(range, in: textFieldValue) {
        let newText = textFieldValue.replacingCharacters(in: textFieldRange, with: string)
        if newText == "" {
            textField.rightView = nil
        } else {
            textField.rightView = iconContainerView
        }
    }

    return true

    }
}
person Joe N    schedule 23.02.2019