ios - Implementing NSUndoManager -
ios - Implementing NSUndoManager -
i'm having hard time implementing nsundomanager, tried reading apple documentation on it, cant figure out. have tried far. have created app draws lines connecting 2 points in array, implemented undo method removing lastly object, cant figure out how implement redo, stumbled upon nsundomanager , started reading documentation, dont know how apply issue. heres code have @ 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 no errors, on runtime when tap on buttons, nil happens. dont understand nsundomanager things go, "object" is. not declaring need declare.
thanks,
the touchesmoved method called multi times when draw line, if register undo action in touchesmoved method need grouping them:
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { [self.undomanager beginundogrouping]; } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { [self.undomanager endundogrouping]; } here's example.
ios objective-c xcode nsundomanager
Comments
Post a Comment