Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Weird Undo Bug

Weird Undo Bug
Thread Tools
Grizzled Veteran
Join Date: Jun 2002
Status: Offline
Reply With Quote
May 2, 2004, 10:31 AM
 
Hey guys,

I have been trying to figure out this undo bug I've been having recently, and thanks to FileMerge, I've found the bad code:

Code:
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"TextTemplate"]) { [docTextView insertText:[[NSUserDefaults standardUserDefaults] objectForKey:@"TextTemplate"]]; }
[All the above is done in awakeFromNib]

Basically, in preferences I have a text view that changes what is inserted on startup into my document NSTextView (docTextView). Now, if I leave the preferences text view empty, undo works on my main document window. But, if I put something into my preferences text view, undo does not work at all on my main document window. So I guess, that doing insertText on startup is somehow messing up the undo system for my main window.

Any ideas appreciated,
Oliver
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
May 2, 2004, 10:44 AM
 
I'm not sure why this isn't working, but here are a couple of things to look at: first, verify that your text view is editable and does actually use undo. Also, I get the impression that your text view is a part of a document, and that you're using NSDocument. If this is indeed the case, try calling [myFrickenDocument updateChangeCount: NSChangeDone] after you insert the text.

Finally, the code you posted, while fine, is sort of inefficient. It calls the user-defaults stuff twice and fetches your key from the user defaults dictionary twice. Here's a more optimized rendition:
Code:
NSString *textTemplate = [[NSUserDefaults standardUserDefaults] stringForKey:@"TextTemplate"]; if ( nil != textTemplate ) [docTextView insertText: textTemplate];
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
May 2, 2004, 10:46 AM
 
Originally posted by smeger:
I'm not sure why this isn't working, but here are a couple of things to look at: first, verify that your text view is editable and does actually use undo. Also, I get the impression that your text view is a part of a document, and that you're using NSDocument. If this is indeed the case, try calling [myFrickenDocument updateChangeCount: NSChangeDone] after you insert the text. The reason for this is that your NSDocument manages the undo stack, and you aren't telling it that something that can be undone was performed.

Finally, the code you posted, while fine, is sort of inefficient. It calls the user-defaults stuff twice and fetches your key from the user defaults dictionary twice. Here's a more optimized rendition:
Code:
NSString *textTemplate = [[NSUserDefaults standardUserDefaults] stringForKey:@"TextTemplate"]; if ( nil != textTemplate ) [docTextView insertText: textTemplate];
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:28 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2