PDA

View Full Version : Darn you Cocoa and your Undoing!! (Help!)




Littleodie914
Jul 30, 2006, 10:59 PM
Hey again fellow Obj-C programmers. I'm on Chapter 7 of Aaron Hillegass' book, and have been trekking along just fine until this point. I can't, for the life of me, understand why there are so many steps just to add undo capabilities to an app. The author seemed to (in previous chapters) explain his steps very thoroughly, but now seems to be just winging out methods left and right.

(And NSEnumerator? He just throws it in the code without even mentioning what it does, let alone what it is!)

So I guess what I'm looking for is a better explanation of NSUndoManager and the methods you need to implement... I've been scouring the web, but have been having trouble finding any examples. Does anyone here have a tutorial-type thing they personally prefer for undo's in Cocoa? Thanks a bunch!

P.S. I'm also getting an error when I compile the RaiseMan program according to Aaron's code... It reads: 2006-07-30 21:46:28.972 RaiseMan[1356] *** -observeValueForKeyPath:ofObject:change:context: only defined for abstract class. Define -[MyDocument observeValueForKeyPath:ofObject:change:context:]!Any ideas on this bugger? :confused:

Thanks again everyone, for all your help. :)



gekko513
Jul 30, 2006, 11:08 PM
Have you defined a method -[MyDocument observeValueForKeyPath:ofObject:change:context:] as the error is asking for?

NSEnumerator lets you go through a list of elements one by one. Typically like so:
NSEnumerator *en = [someArray objectEnumerator];
id obj;
while (obj = [en nextObject]) {
// do something with or to the object here
}

The while loop will loop through all the objects in someArray starting at the first object ([someArray reverseObjectEnumerator] would start at the last object).

mduser63
Jul 30, 2006, 11:18 PM
I had some trouble with the Undo stuff in Hillegass's book too, for the exact reason you describe. I don't have any specific advice. Just go back through it and carefully try to figure it all out. If it makes you feel any better, I was able to figure it out, and after some serious study, it all makes good sense. It'll probably be the same for you.

As for NSEnumerator, it sounds like you didn't go through an Objective-C book first. That might be a good idea. NSEnumerator can be pretty handy. Stephen Kochan's Programming in Objective-C is the most recommended book around here, but there are certainly others. He specifically covers NSEnumerator (along with a whole bunch of other useful stuff).

caveman_uk
Jul 31, 2006, 03:12 AM
Try this document (http://www.ex-cinder.com/downloads/undo-article.rtf) for tips on undo. Undo isn't that hard - you just need to record each action you do to your data. How you do that is up to you.

Littleodie914
Jul 31, 2006, 12:00 PM
Try this document (http://www.ex-cinder.com/downloads/undo-article.rtf) for tips on undo. Undo isn't that hard - you just need to record each action you do to your data. How you do that is up to you.Haha I actually found that article last night an hour or so after posting, seems to be really descriptive and example-ish. I like it, and have bookmarked it. :)

Littleodie914
Jul 31, 2006, 12:36 PM
Alright, I've got one more question... In the RaiseMan example, (sorry to those who aren't familiar with this) the methods in MyDocument.m are executed when the user inserts or deletes people from the table.

Why is this? There haven't been any outlets/actions set with the "Add Person" button to run the - (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index method... How does it know to do it automcatically?

I guess I could understand it being able to look for an insertObject method every time, but how would it expect there to be an inEmployeesAtIndex argument as well? I'm really just wondering why those methods are called, even if I haven't linked them to the buttons, or vice versa. Thanks!! :D

Kelmon
Aug 1, 2006, 10:34 AM
If I remember the chapter correctly the addition/removal of people from the application is performed courtesy of the ArrayController that is bound to the table and the array of employees. In this respect no IBActions are required because the ArrayController knows how to add and remove objects from the array that it is managing.

Does that help?