Episode 12 - NSUndoManager
Posted 01/28/2007 - 21:48 by cocoacast
The Undo is a powerful tool that is easy to add to your application. We will Show how to add the Undo to Cocoa Applications using NSUndoManager and NSInvocation. We will modify the Advanced RaiseMan application created in previous podcast and give some new challenges for you to work on. We will also talk about MacWorld 2007, iPhone, and other mac-related topics.
Here are the show notes for this Episode: Show Notes
View the Podcast
Show Notes:
Adding Undo to your Apps
Episode 12
NSUndoManager - adding undo capabilities to your app in elegant manner
Chapter 7
Adding Undo to your Apps
Episode 12
NSUndoManager - adding undo capabilities to your app in elegant manner
Chapter 7
Podcast Announcements
• Mac World 2007 is over
• iPhone and what it means to all of us
• Your participation
• Late Night Cocoa (www.latenightcocoa.com)
Podcast Announcements
• Mac World 2007 is over
• iPhone and what it means to all of us
• Your participation
• Late Night Cocoa (www.latenightcocoa.com)
Recap of the Last Episode
Summary of the Episode 11
Recap of the Last Episode
Summary of the Episode 11
Summary of Episode 11
• Advanced Cocoa Bindings
• RaiseMan without Bindings - Challenge 2 from Chapter 6
• Expanded RaiseMan
• Other Bindings-related topics
• CoreData RaiseMan
Summary of Episode 11
• Advanced Cocoa Bindings
• RaiseMan without Bindings - Challenge 2 from Chapter 6
• Expanded RaiseMan
• Other Bindings-related topics
• CoreData RaiseMan
In this Episode
• Adding Undo to Your Applications
• NSInvocation
• NSUndoManager
• RaiseMan with the Undo
• Key-Value Observing (KVO)
• Undo for Edits
• Windows and the UndoManager
In this Episode
• Adding Undo to Your Applications
• NSInvocation
• NSUndoManager
• RaiseMan with the Undo
• Key-Value Observing (KVO)
• Undo for Edits
• Windows and the UndoManager
NSInvocation
A way to package up a message
NSInvocation
A way to package up a message
NSInvocation
• Used to package a message (selector, receiver, and all arguments)
• Used in message forwarding
- (void)forwardInvocation:(NSInvocation *)x
• In order to call forwardInvocation: we need message signature
- (NSMethodSignature *)methodSignature:(SEL)aSel
NSInvocation
• Used to package a message (selector, receiver, and all arguments)
• Used in message forwarding
- (void)forwardInvocation:(NSInvocation *)x
• In order to call forwardInvocation: we need message signature
- (NSMethodSignature *)methodSignature:(SEL)aSel
NSInvocation
- (void)forwardInvocation:(NSInvocation *)invocation{
SEL aSelector = [invocation selector];
if ([helper respondsToSelector:aSelector])
[invocation invokeWithTarget:helper];
else
[self doesNotRecognizeSelector:aSelector];
}
- (BOOL)respondsToSelector:(SEL)aSelector{
BOOL result = [super respondsToSelector:aSelector];
if (result == NO) {
return = [helper respondsToSelector:aSelector];
}
return result;
}
NSInvocation
- (void)forwardInvocation:(NSInvocation *)invocation{
SEL aSelector = [invocation selector];
if ([helper respondsToSelector:aSelector])
[invocation invokeWithTarget:helper];
else
[self doesNotRecognizeSelector:aSelector];
}
- (BOOL)respondsToSelector:(SEL)aSelector{
BOOL result = [super respondsToSelector:aSelector];
if (result == NO) {
return = [helper respondsToSelector:aSelector];
}
return result;
}
NSInvocation
// This gets run before forwardInvocation:
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector)
{
NSMethodSignature *result;
result = [super methodSignatureForSelector:aSelector];
if (!result) {
result = [helper methodSignatureForSelector:aSelector];
}
return result;
}
NSInvocation
// This gets run before forwardInvocation:
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector)
{
NSMethodSignature *result;
result = [super methodSignatureForSelector:aSelector];
if (!result) {
result = [helper methodSignatureForSelector:aSelector];
}
return result;
}
How NSUndoManager Works
The Undo and Redo stacks
How NSUndoManager Works
The Undo and Redo stacks
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
NSInvocation
Delete First Record
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
NSInvocation
Delete First Record
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set name to “New Employee”
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set name to “New Employee”
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set name to “New Employee”
NSInvocation
Set raise to 0
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
• Changes the raise from zero to 20
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set name to “New Employee”
NSInvocation
Set raise to 0
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set name to “New Employee”
NSInvocation
Set raise to 20
How the NSUndoManager Works
• Insert a new record
• Changes the name from “New Employee” to “Rex Fido”
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set name to “New Employee”
NSInvocation
Set raise to 20
How the NSUndoManager Works
• Insert a new record
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set raise to 20
NSInvocation
Set name to “Rex Fido”
How the NSUndoManager Works
• Insert a new record
Undo stack Redo stack
NSInvocation
Delete First Record
NSInvocation
Set raise to 20
NSInvocation
Set name to “Rex Fido”
How the NSUndoManager Works
Undo stack Redo stack
NSInvocation
Insert a new record
NSInvocation
Set raise to 20
NSInvocation
Set name to “Rex Fido”
How the NSUndoManager Works
Undo stack Redo stack
NSInvocation
Insert a new record
NSInvocation
Set raise to 20
NSInvocation
Set name to “Rex Fido”
NSUndoManager - mirror methods
• prepareWithInvocationTarget: notes the target and returns undo man.
• overrides forwardInvocation: so it adds specific invocation to stack
- (void)makeItHotter {
temperature = temperature + 10;
[[undoManager prepareWithInvocationTarget:self]
makeItColder];
[self showTheChangesToTheTemperature];
}
- (void)makeItColder {
temperature = temperature - 10;
[[undoManager prepareWithInvocationTarget:self]
makeItHotter];
[self showTheChangesToTheTemperature];
}
NSUndoManager - mirror methods
• prepareWithInvocationTarget: notes the target and returns undo man.
• overrides forwardInvocation: so it adds specific invocation to stack
- (void)makeItHotter {
temperature = temperature + 10;
[[undoManager prepareWithInvocationTarget:self]
makeItColder];
[self showTheChangesToTheTemperature];
}
- (void)makeItColder {
temperature = temperature - 10;
[[undoManager prepareWithInvocationTarget:self]
makeItHotter];
[self showTheChangesToTheTemperature];
}
Demo
Adding Undo to RaiseMan
Demo
Adding Undo to RaiseMan
Key-Value Observing (KVO)
Stay informed about changes made to variables (using KVC)
Key-Value Observing (KVO)
Stay informed about changes made to variables (using KVC)
Key Value Observing (KVO)
• A method in NSObject allows to register listener for changes
• When a change occurs a message is sent
- (void)addObserver:(NSObject *)observerforKeyPath:(NSString *)keyPathoptions:(NSKeyValueObservingOptions)options
context:(void *)context;
- (void)observeValueForKeyPath:(NSString *)keyPathofObject:(id)objectchange:(NSDictionary *)change
context:(void *)context;
Key Value Observing (KVO)
• A method in NSObject allows to register listener for changes
• When a change occurs a message is sent
- (void)addObserver:(NSObject *)observerforKeyPath:(NSString *)keyPathoptions:(NSKeyValueObservingOptions)options
context:(void *)context;
- (void)observeValueForKeyPath:(NSString *)keyPathofObject:(id)objectchange:(NSDictionary *)change
context:(void *)context;
Demo
Undo for Edits
Demo
Undo for Edits
Windows and the Undo Manager
Views can add edits to the undo manager
Windows and the Undo Manager
Views can add edits to the undo manager
NSInvocation
• First, ask delegate
- (NSUndoManager *)undoManagerForTextView:(NSTextView *)tv
• Second, ask window
- (NSUndoManager *)undoManager
• Window’s delegate can supply an undo manager by implementing
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
NSInvocation
• First, ask delegate
- (NSUndoManager *)undoManagerForTextView:(NSTextView *)tv
• Second, ask window
- (NSUndoManager *)undoManager
• Window’s delegate can supply an undo manager by implementing
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
Challenges
• Add undo to Advanced RaiseMan Application
• Create a Cocoa Binding version of the Advanced RaiseMan -- the Undo should work
• Create a CoreData version of the Advanced RaiseMan -- the Undo should work too
Challenges
• Add undo to Advanced RaiseMan Application
• Create a Cocoa Binding version of the Advanced RaiseMan -- the Undo should work
• Create a CoreData version of the Advanced RaiseMan -- the Undo should work too
Summary
• Adding Undo to Your Applications
• NSInvocation
• NSUndoManager
• RaiseMan with the Undo
• Key-Value Observing (KVO)
• Undo for Edits
• Windows and the UndoManager
Summary
• Adding Undo to Your Applications
• NSInvocation
• NSUndoManager
• RaiseMan with the Undo
• Key-Value Observing (KVO)
• Undo for Edits
• Windows and the UndoManager
Next Time
• Archiving
• NSCoder and NSCoding
• Encoding
• Decoding
• Document Architecture
• Info.plist and NSDocumentController
• NSDocument
• Saving and Loading
• More...
Next Time
• Archiving
• NSCoder and NSCoding
• Encoding
• Decoding
• Document Architecture
• Info.plist and NSDocumentController
• NSDocument
• Saving and Loading
• More...
COMMENTS
• Send us comments
• Voice
• eMail
• Forum
• Be active!!!
COMMENTS
• Send us comments
• Voice
• eMail
• Forum
• Be active!!!
The Best Code is the Code...
you do not have to Write
The Best Code is the Code...
you do not have to Write





Undo is really a great tool and indeed it has extended the functionality for Cocoa, I am happy to find so many useful information about it. Thanks for sharing.
cheap flights to indianapolis cheap flights to dallas
How to suffer in challenges . Challenges
• Add undo to Advanced RaiseMan Application
• Create a Cocoa Binding version of the Advanced hid kit RaiseMan -- the Undo should work
• Create a CoreData version of the Advanced RaiseMan -- the Undo should work too .