Undo – Redo using NSUndoManager

Suppose you have a set of actions to be performed and you want o undo the changes, you can use NSUndoManager.

NSUndoManager  piles all the actions on to a stack so that you when you want to undo them, the action that is last added to the stack is popped out and you get the previous action.

It keeps Undo as well as the Redo stack.

Code below allocs the NSUndoManager object.

NSUndoManager *undoManager = [[NSUndoManager alloc] init];

First you need to register the object that you want to Undo with the NSUndoManager object.

[[undoManager prepareWithInvocationTarget:self] user_method:object];

where user_method: is the action that is recorded by the undoManager object which it uses to undo and redo the action.

NSUndoManager takes care of all the changes by adding these actions to its undo and redo stack.

To undo an action, simply call “undo” on the NSUndoManager object.

[undoManager undo];

To redo an action, you call “redo” on NSUndoManager object.

[undoManager redo];

To check if you can undo an action, you can use canUndo on NSUndoManager object.

[undoManager canUndo];

to check if you can redo an action, you can use canRedo on NSUndoManager object.

[undoManager canRedo];

To set number of levels of undo, use setLevelsOfUndo

[undoManager setLevelsOfUndo:20];

How to make a view follow an elliptical path.

If |x1−x2|>|y1−y2|, then the line will intersect the square in one of the sides, left or right.

Then if x1>x2, the point of intersection is on the left, so the point will be some (x,y) with x=x2−15.

Since it is on the line (x2,y2)+λ(x2−x1,y2−y1) we know that for some λ, x2−15=x2+λ(x2−x1) and y=y2+λ(y2−y1). The first equality gives you λ=15/(x1−x2) so that the second equality gives you y, i.e. y=y2−15y1−y2x1−x2.

So the point of intersection is (x2−15,y2−15y1−y2x1−x2).

You can do the same for x1<x2, when you get x=x2+15, while for |x1−x2|<|y1−y2| you know that the intersection is either at the top or at the bottom of the square, giving a similar analysis.

Creating a new NavigationBar and adding a left label and right navigation Item

//add a UInavigation bar

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:


navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

navBar.barStyle = UIBarStyleBlackTranslucent;

[self.view addSubview:navBar];

[navBar release];

//add a label to the navbar

UILabel *label = [[UILabel alloc] initWithFrame:


label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

label.text = @”Score 0/0″;

label.backgroundColor = [UIColor clearColor];

label.textColor =[UIColor whiteColor];

label.font=[UIFont boldSystemFontOfSize:16];

label.textAlignment = UITextAlignmentCenter;

[navBar addSubview:label];

[label release];

//add a right hand side navigation button to the navbar

UINavigationItem *navItem;

navItem = [UINavigationItem alloc];

UIBarButtonItem *tempButton = [[UIBarButtonItem alloc]


target:self action:@selector(saveNext:) ];

navItem.rightBarButtonItem = tempButton;

[navBar pushNavigationItem:navItem animated:false];

[navBar setDelegate:self];

[navItem release];

[tempButton release];

Back Button in Child View Controller Pointing to the Parent Controller with the text “Back”.

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 30.0)];

[button setImage:backNormal forState:UIControlStateNormal];

[button setImage:backPressed forState:UIControlStateHighlighted];

[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];

[button release];

self.navigationItem.leftBarButtonItem = backButton;

[backButton release];

Modal View doen’t uses navigation bar o …

Modal View doesn’t uses navigation bar of its parent view.
Solution, you need to add a separate navigation bar on the modal view.

iPhone Navigation-Based application

If you have unhide the toolbar of the NavigationController than you can set the items of toolbar in other ViewControllers as follows:

UIImage *homeImage = [UIImage imageNamed:@”home.png”];
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithImage:homeImage style:UIBarButtonItemStyleBordered target:self action:@selector(clicked)];
UIBarButtonItem *flexibleBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
flexibleBarButton.width = 400;
NSArray *buttonArray = [[NSArray alloc] initWithObjects:flexibleBarButton,homeButton,nil];
self.toolbarItems = buttonArray;
[buttonArray release];
[flexibleBarButton release];
[homeButton release];