There is a normal ViewController , and its self.view added 10-15 normal self.view . As after all actions are completed with these ViewController (moving them within the ViewController - a), you ViewController update the ViewController , returning it to the initial state, in principle, you do not even need to save anything.

The setNeedsDisplay method, which is responsible for view redrawing, somehow does not work. And calling viewDidLoad is probably not the best solution.

  • what exactly is meant by the initial state? remove all these views? - Max Mikheyenko
  • @MaxMikheyenko, Hello! I mean the return of all view to its original position on self.view. Naturally, without animation. How to "restart" ViewController -a. - Andrey COYG
  • you just need to take out the installation of frames in a separate method from the viewDidLoad method and call it when redrawing is necessary. For a more complete answer, I need to see the contents of the viewDidLoad methodAleksey Alybin

2 answers 2

UIView has a subviews property that is NSArray. Therefore, to solve your problem, you need to run through all the subviews and call removeFromSuperview for each.

 for (UIView *subView in self.view.subviews) { [subView removeFromSuperview]; } 

If you need to restore the position on the view after moving, then save the locations of all subviews to the archive and then restore

 //сохранить NSMutableArray *subViewFramesArray = [NSMutableArray array]; for (UIView *subView in self.view.subviews) { [subViewFramesArray addObject:[NSValue valueWithCGRect:subView.frame]]; } //восстановить int i = 0; for (UIView *subView in self.view.subviews) { [subView setFrame:[[subViewFramesArray objectAtIndex:i] CGRectValue]]; i++; } 
  • in the commentary to the question, the author indicated that the view should not be removed, but returned to the initial position - Max Mikheyenko
  • In this case, you need to store the frame of each subView in an array and then restore everything like this - Alexander Yolkin

I suggest that you simply recreate the viewController and replace the current one with a new one.

If you have one controller:

 ViewController *myViewController = [[ViewController alloc] init]; [[UIApplication sharedApplication] keyWindow].rootViewController = myViewController; 

if you have a hierarchy managed by navigationController:

 ViewController *myViewController = [[ViewController alloc] init]; [self.navigationController popViewControllerAnimated:NO]; [self.navigationController pushViewController:myViewController animated:NO];