本文介绍了实现NSUndoManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难实现NSUndoManager,我尝试阅读它上面的苹果文档,但我无法弄明白。这是我到目前为止所尝试的。我创建了一个通过连接数组中的两个点绘制线条的应用程序,我通过删除最后一个对象实现了一个撤消方法,但是无法弄清楚如何实现重做,我偶然发现了NSUndoManager并开始阅读其文档,但我不这样做知道如何将它应用到我的问题。这是我目前的代码

I'm having a hard time implementing the NSUndoManager, I tried reading the apple documentation on it, but I cant figure it out. This is what I have tried so far. I have created an app that draws lines by connecting two points in an array, I implemented an undo method by removing the last object, but cant figure out how to implement the redo, I stumbled upon NSUndoManager and started reading its documentation, but I dont know how to apply it to my issue. Heres the code that I have at the moment

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSUInteger taps = [[touches anyObject]tapCount];
    if(taps == 2) {
        [self setNeedsDisplay];
    }
    else {
        if([self.pointsArray count] == 0) {
            self.pointsArray = [[NSMutableArray alloc]init];
            UITouch *t = [touches anyObject];
            CGPoint startLoc = [t locationInView:self];
            [self.pointsArray addObject:[NSValue valueWithCGPoint:startLoc]];
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *t = [touches anyObject];
    CGPoint currentLoc = [t locationInView:self];
    [self.pointsArray addObject:[NSValue valueWithCGPoint:currentLoc]];
    [self setNeedsDisplay];
}

#pragma mark - Undo/Redo Methods
-(void)undo:(id) object {
    [[undoManager prepareWithInvocationTarget:self]redo:object];
    [undoManager setActionName:@"undoLineSegment"];
    [self.pointsArray removeLastObject];
}

-(void)redo:(id)object {
    [self.pointsArray addObject:object];
    [[undoManager prepareWithInvocationTarget:self]undo:object];
    [undoManager setActionName:@"RedoUndoneLineSegment"];
}

- (IBAction)undoButton:(UIButton *)sender {
    [self.undoManager undo];
    [self setNeedsDisplay];
}

- (IBAction)redoButton:(UIButton *)sender {
    [self.undoManager redo];
    [self setNeedsDisplay];
}

我没有错误,但在运行时点按按钮,没有发生。关于NSUndoManager,我不了解的是事情的发展方向,对象是什么。我没有声明我需要声明的内容。

I get no errors, but on runtime when I tap on the buttons, nothing happens. What I dont understand about NSUndoManager is where things go, what the "object" is. Am I not declaring something that I need to declare.

谢谢,

推荐答案

Swift 4中的完整示例。* Xcode 9.3

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var colorsCollectionView: UICollectionView!

    @IBOutlet weak var colorView: UIView!

    var colorsArray = ["green","blue","red","black","yellow","brown","orange","purple","cyan","magenta"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

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

    func changeColor(color: UIColor) {

        let oldColor = self.colorView.backgroundColor ?? UIColor.white
        undoManager?.registerUndo(withTarget: self, handler: { (targetSelf) in
            targetSelf.changeColor(color: oldColor)
        })
        colorView.backgroundColor = color
    }

}


extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colorsArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)

        let label:UILabel = cell.viewWithTag(101) as! UILabel
        label.text = colorsArray[indexPath.row]

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let colorName = colorsArray[indexPath.row]

        switch colorName {
        case "green":
            changeColor(color: UIColor.green)
        case "blue":
            changeColor(color: UIColor.blue)
        case "red":
            changeColor(color: UIColor.red)
        case "black":
            changeColor(color: UIColor.black)
        case "yellow":
            changeColor(color: UIColor.yellow)
        case "brown":
            changeColor(color: UIColor.brown)
        case "orange":
            changeColor(color: UIColor.orange)
        case "purple":
            changeColor(color: UIColor.purple)
        case "cyan":
            changeColor(color: UIColor.cyan)
        case "magenta":
            changeColor(color: UIColor.magenta)
        default:
            changeColor(color: UIColor.white)
        }
    }
}

这篇关于实现NSUndoManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:01