Implementing NSUndoManager

旧城冷巷雨未停 提交于 2019-12-04 16:03:26

问题


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];
}

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.

Thanks,


回答1:


The touchesMoved method will called multi times when you draw a line, so if you register undo action in the touchesMoved method you need to group them:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.undoManager beginUndoGrouping];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.undoManager endUndoGrouping];
}

here's a example.




回答2:


Complete example in Swift 4.* and 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)
        }
    }
}



来源:https://stackoverflow.com/questions/24439803/implementing-nsundomanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!