I'm sure there's a simple answer to this, but I'm pretty noob to programming and have searched extensively here and on the few Undo tutorials I could find.
I have two buttons and a UITextField with a number in it. The buttons increment the number in the textfield up or down by 1 respectively. I have several more sets of these two buttons and a textfield, each representing something different. Then I have undo and redo buttons that undo the changes to the text fields caused by pressing the buttons. This all works great.
So now I'm trying to add the ability to undo changes from editing the text field directly with the number pad, so that it goes into the same undo stack and can be undone and redone from the same undo and redo buttons.
Here's a snippet of the code that controls the undo and redo of the button presses. Then, like I said, I have several more sets of these that all go into the same undo stack. Thanks.
The UITextField
@IBOutlet weak var Money: UITextField!
The Undo and Redo Buttons
@IBAction func Undo(_ sender: UIButton)
{
undoManager?.undo()
}
@IBAction func Redo(_ sender: UIButton)
{
undoManager?.redo()
}
The Up One and Down One Buttons
@IBAction func MonDown(_ sender: UIButton)
{
subtractOneMon(Mon: Money)
}
@IBAction func MonUp(_ sender: UIButton)
{
addOneMon(Mon: Money)
}
The Functions Registering the Undo/Redo
func subtractOneMon(Mon: UITextField)
{
undoManager?.registerUndo(withTarget: self, handler:
{(targetSelf) in
targetSelf.addOneMon(Mon: self.Money)
})
let Mon = Double(Money.text!)
let NewMon = Double(Mon! - 1)
Money.text = Int(NewMon).description
}
func addOneMon(Mon: UITextField)
{
undoManager?.registerUndo(withTarget: self, handler:
{(targetSelf) in
targetSelf.subtractOneMon(Mon: self.Money)
})
let Mon = Double(Money.text!) ?? 0
let NewMon = Double(Mon + 1)
Money.text = Int(NewMon).description
}