这是我第一次尝试使用nsundommanager,我不确定我应该把它放在哪里/我错过了什么。我在做一个应用程序它有一个地图视图有一个绘制按钮和撤消按钮。我让绘制按钮使用户能够绘制折线,每次他们的手指离开屏幕时,它就会将另一条线连接到他们刚刚绘制的线上。我想让撤销按钮去掉最后画的线。
到目前为止我所知道的…
- (void)viewDidLoad
{
[super viewDidLoad];
...
...
undoManager = [[NSUndoManager alloc]init];
}
- (IBAction)oopsButton:(id)sender {
[undoManager undo];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
....
....
[[undoManager prepareWithInvocationTarget:self]touchesEnded:(touches) withEvent:event];
[undoManager setActionName:NSLocalizedString(@"Route Changed", @"route undo")];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
...
...
现在,当我点击撤销,它撤销一行,但仅此而已。我需要它在用户点击时保持撤销。谢谢你的帮助,我已经找遍了谷歌和堆栈,还没有找到任何有用的东西。
我找到了一种方法来更新折线而不使用撤销管理器…
- (IBAction)oopsButton:(id)sender {
//remove last point from array
[self.coordinates removeLastObject];
//update line on map
NSInteger numberOfPoints = [self.coordinates count];
if (numberOfPoints > 1)
{
MKPolyline *oldPolyLine = self.polyLine;
CLLocationCoordinate2D points[numberOfPoints];
for (NSInteger i = 0; i < numberOfPoints; i++)
points[i] = [self.coordinates[i] MKCoordinateValue];
MKPolyline *newPolyLine = [MKPolyline polylineWithCoordinates:points count:numberOfPoints];
[self.mapView addOverlay:newPolyLine];
self.polyLine = newPolyLine;
if (oldPolyLine)
[self.mapView removeOverlay:oldPolyLine];
}
}