撤消重做操作

Undo redo Operation

我使用了以下代码来进行撤消重做操作。我正在更改标签的背景颜色并且我的 undo/redo 操作工作正常但现在我已经添加了标签的文本颜色所以每当我单独更改背景颜色或文本颜色时它都可以正常工作但是如果我同时更改背景颜色和文本颜色一次,它将无法正常工作。我应该怎么办?请帮助.!

@IBOutlet var myObject: UILabel!

//  MARK: - View Life Cycle
override func viewDidLoad() {
    super.viewDidLoad()
    myObject.backgroundColor = UIColor.yellow
}

func setMyObjectColor(_ color: UIColor) {
        (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setMyObjectColor(myObject.backgroundColor!)
        myObject.backgroundColor = color
}

func setMyObjectTextColor(_ color: UIColor) {
        (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setMyObjectTextColor(myObject.textColor!)
        myObject.textColor = color
}

@IBAction func changeColor(_ sender: Any){
    setMyObjectColor(UIColor.blue)
}

@IBAction func changeTextColor(_ sender: Any){
    setMyObjectTextColor(UIColor.white)
}

@IBAction func undo(_ sender: Any) {
    undoManager?.undo()
}

@IBAction func redo(_ sender: Any) {
    undoManager?.redo()
}}
@IBOutlet var myObject: UILabel!

//  MARK: - View Life Cycle
override func viewDidLoad() {
    super.viewDidLoad()
    myObject.backgroundColor = UIColor.yellow
}

func setMyObjectColor(_ Backcolor: UIColor) {
        (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setMyObjectColor(myObject.backgroundColor!)
        myObject.backgroundColor = Backcolor
}

func setMyObjectTextColor(_ Textcolor: UIColor) {
        (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setMyObjectTextColor(myObject.textColor!)
        myObject.textColor = Textcolor
}

func setMyObjectText(_ Text: String) {
    (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setMyObjectText((myObject.text! as AnyObject) as! String)
    myObject.text = Text
}

@IBAction func changeColor(_ sender: Any){
    setMyObjectColor(UIColor.blue)
}

@IBAction func changeTextColor(_ sender: Any){
    setMyObjectTextColor(UIColor.white)
}

@IBAction func changeText(_ sender: Any) {
    setMyObjectText("Khush")
}

@IBAction func undo(_ sender: Any) {
    undoManager?.undo()
}

@IBAction func redo(_ sender: Any) {
    undoManager?.redo()
}

使用此代码我的问题已解决。