Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-22-2011, 08:29 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: Geneva, switzerland
Posts: 6
elio.d is on a distinguished road
Question UitextView + NSUndoManager

I'm trying to integrate the undo/redo features in a UITextView (I'm building a LateX editor) with no luck and to be honest I'm really confused about this argument. I don't understand the steps involved in those two operations, I mean I need two methods
- one to remove the last inserted text
- one to restore the text removed

one doubt is where I get the last inserted text? in other words where I have to register for the undo?

- in textViewDidChange I can get the whole text
- in textViewShouldChangeTextInRange I can get the last char inserted

I know that what I write isn't the best explanation ever, But I hope someone here has faced the same problem in the past and can give me an hint. Basically ,to resume, I have to add the undo/redo features to a textview.

Thanks in advance
elio.d is offline   Reply With Quote
Old 02-25-2011, 03:20 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: Geneva, switzerland
Posts: 6
elio.d is on a distinguished road
Default

a little bump with an update. So my problem was to make my textView capable of undo/redo action ( I use two buttons to do this ). Reading the doc I discovered that UITextView has a built-in undoManager and his basic usage is really simple. What I've done so far?

I've a viewController (EditorViewController) containing the textView.

- in EditorViewcontroller.h
Code:
 NSUndoManager *myUndoManager;
- in EditorViewController.m --> viewDidLoad
Code:
 myUndoManager = [textView undoManager];
as I said two buttons are used to perform undo/redo actions, thos two buttons are located into the inputAccessoryView of the textView, this view is basically a toolbar with several button used to append text to the textView.
I've a method called appendText:
Code:
- (IBAction) appendText:(id)sender{
	
	NSString *contentsToAdd;
	NSMutableString *textViewContent;
	NSRange cursorPosition;
	if ([undoManager canUndo]) {
		NSLog(@"yes canundo");
	}
	switch ([sender tag]) {
	  case 0:
	    [textView setScrollEnabled:NO];
	    
            contentsToAdd = @"[]";   
	    
            cursorPosition = [textView selectedRange];
            
            textViewContent = [[NSMutableString alloc] 
                                 initWithString:[textView text]];
	    
            [textViewContent insertString:contentsToAdd 
                                          atIndex:cursorPosition.location];
	    
           [textView setText:tfContent];

            [tfContent release];
	    cursorPosition.location++;
            textView.selectedRange=cursorPosition;

	    [textView becomeFirstResponder];
	    [textView setScrollEnabled:YES];
	     if (![undoManager canUndo]) {
		    NSLog(@" can't undo");
	     }	
	     break;
            // more case following 0..9
            case 10:
             [myUndoManager undo];
             [break];  
            case 11 :
             [myUndoManager redo];
             break;
}
Now things works well if I write using the keyboard, I mean undo and redo works properly. But when I append some text using the appendText: method, undo and redo aren't performed.If I begin to write again using the keyboard undo and redo are performed(the first element of the undo stack is last text written) It's like if the undo and redo stack are cleared every time I append some text. I hope someone can give me an hint..
elio.d is offline   Reply With Quote
Old 07-08-2011, 05:04 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 1
macguru17 is on a distinguished road
Default

I've found a working solution and thought you might be interested in it. To avoid double-posting, please have a look at my answer on Stack Overflow.
macguru17 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsundomanager, redo, uitextview, undo

Thread Tools
Display Modes

Posting 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



» Advertisements
» Online Users: 378
17 members and 361 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0