Delegation method called twice in Xcode 8.1 / Swift 3.0 with a debugger breakpoint

I have a problem with xcode 8.1 / swift 3. I think it might be a bug in the Xcode debugger software, but I thought I would see if anyone sees something that I don’t see. I am working on an ios application where I need to override the backspace method used with UITextField. I am setting up a delegate to keep track of the backspace key pressed. The program runs until I set a breakpoint for the debugger in the overridden method (see code below). When I do this, the backspace method is called twice for each time I press a key, and not once. If I remove the breakpoint, the code works. Is this just a mistake, or am I doing something wrong? Here is the code, thanks for your help!

import UIKit

class ViewController: UIViewController, EqFieldDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    let newField = EqField()
    newField.myDelegate = self
    newField.becomeFirstResponder()
    view.addSubview(newField)
    newField.frame = CGRect(x: 50, y: 50, width: 200, height: 150)

}

func backspacePressed( ){

    print("in backspacePressed") // breakpoint added here
}

}   

UITextField:

import UIKit

class EqField: UITextField {

    var myDelegate: EqFieldDelegate?

    override func deleteBackward() {
        super.deleteBackward()
        myDelegate?.backspacePressed()
    }
}

protocol EqFieldDelegate {
    func backspacePressed()
}
+4
1

, .

, , . , Apple:

  • backspacePressed
  • 0,5 , , backspacePressed
  • 0,1 , , backspacePressed

:

  • viewDidLoad

    let button = UIButton(type: .system)
    button.setTitle("Tap Me", for: .normal)
    button.addTarget(self, action: #selector(ViewController.handleButtonTouched(button:)), for: .touchDown)
    button.addTarget(self, action: #selector(ViewController.handleButtonReleased(button:)), for: .touchUpInside)
    button.frame = CGRect(x: 50, y: newField.frame.maxY, width: 200, height: 150)
    view.addSubview(button)
    
  • viewDidLoad

    var buttonTimer: Timer?
    
    func handleButtonTouched(button: UIButton) {
        buttonTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false, block: { [weak self] timer in
    
            print("button been held down for a while!")
            self?.backspacePressed()
    
            self?.buttonTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (timer) in
                print("button still held down")
                self?.backspacePressed()
            })
    
        })
        print("button was pressed") // add a breakpoint here
        self.backspacePressed()
    }
    
    
    func handleButtonReleased(button: UIButton) {
        buttonTimer?.invalidate()
    }
    
  • () ,

    func backspacePressed( ){
        //print("in backspacePressed")
    } 
    

, , , :

button was pressed
button been held down for a while!
button still held down
button still held down
button still held down
button still held down

( ),

button was pressed

, , , , :

button was pressed
button been held down for a while!

, , . , , , , , , .

, - - ? , , , - handleButtonReleased , .

, Apple - , , , - , , ! . , , , , .

+1

Source: https://habr.com/ru/post/1661097/


All Articles