我使用以下代码进行撤消重做操作。我正在更改标签的背景颜色,我的撤消/重做操作工作正常,但现在我添加了标签的文本颜色,所以每当我单独更改背景颜色或文本颜色时,它工作正常,但如果我改变背景颜色和文本颜色时间那么它将无法正常工作。我该怎么办?请帮忙。!
@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()
}}
答案 0 :(得分:1)
@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()
}
使用此代码解决了我的问题。