I have a project where I am adding selected images to a UIView. Once I add an image I can move, rotate, and zoom that image. But let’s say I picked the wrong image and I want to undo. What would be the proper method to handle something like this? Also, let’s say I add multiple but want to clear all of them from the view, what would be the proper method for this?
Here is what I am trying so far:
-(IBAction)undoButtonTapped
{
self.stampedImageView.image = nil;
}
This will successfully undo the previous action but only that. Let’s say I add two or more images. Maybe I want to undo one or more action but not all of them. This will only undo the most recent action.
Example:
User adds three stamps
To undo 2 stamps: tap the undo button twice
To delete all stamps: tap the delete button once
I have figured out how to remove all stamps with one button tap. See below.
Delete Code:
UIView * subview;
while ((subview = [[imageView subviews] lastObject]) != nil)
{
[subview removeFromSuperview];
}
You can loop through your view’s subviews, testing for whether a particular subview is an image view and then call removeFromSuperview on as many as you want. The views in the subviews will be in the same order as they were added, so to remove the last n items, you need to be enumerate the array of image views from the end. As an example, this method would delete the last 3 image views added: