0

I have an initial view controller, say vc1. It has modal buttons, which when selected, takes me to the second view controller, vc2. Inside this view controller (vc2), i have defined a view, say 'view'. Now, After performing some animations, I want to go back to vc1. IS this possible?

I have been able to dismiss the current view using:

    [view removeFromSuperview];

But I want to go back to the previous view controller vc1. According to this post: Get to UIViewController from UIView?, I understand that I can get the current UIViewController. But how do I go back to the previous view controller?

Community
  • 1
  • 1
vasupradha
  • 35
  • 10

3 Answers3

2

First, as discussed in the post you referenced, we "should not need to access the view controller directly", I think this will go on the opposite direction of MVC.

And I am not sure how you want to go back to vc1, is this triggered by pressing some button? If you want to go back, you can do this in vc2 rather than the view. You see, in MVC, views just show UI which view controller tell it to do and responds to actions from user, and then view controller handles all the logic, so when you want to go back to vc1, I think it is proper to place the control code in vc2, just bind some action to the view, like a @selector or a block, then the view in vc2 can trigger this, and there you go.

Jiaru
  • 156
  • 2
  • okay then, can i call a uiview subclass from a uiviewcontroller subclass? – vasupradha Nov 17 '14 at 07:19
  • Reply to vasupradha (sorry I don't know how to mention you in this comment): It is much easier to access view of subview in VC. Access view (or subview) directly if they are @property of the VC or VC's view, or using [UIView viewWithTag:], official Doc says that "This method searches the current view and all of its subviews for the specified view". – Jiaru Nov 17 '14 at 07:31
1

I think you're getting Views vs. ViewControllers mixed up. I don't think you need vc2 at all.

Try this in vc1:

  • on button press, create a new view (not a view controller)
  • add the view as a subview to self.view
  • do the animation
  • remove the new view from a completion block attached to the animation
Anna Dickinson
  • 3,217
  • 21
  • 42
  • Hey Anna, I did what you said, it is working perfectly... Now my view is on to of vc1. On clicking a button thats in vc1, my view now loads. I have given [view removeFromSuperview];to remove the view. Now, a blank screen appears, but not my vc1. How do i make the vc1 appear? – vasupradha Nov 19 '14 at 20:51
  • Are you removing your subview, or vc1's view (self.view)? You want to remove the subview only. If that's not it, you need to refresh self.view after you remove the subview. Try one (or more) of these: [self.view setNeedsDisplay] and/or [self.view layoutIfNeeded] – Anna Dickinson Nov 21 '14 at 10:13
  • I was kind of removing the rrot view from the view controller. I removed the subview and now, it all works fine... Many many thanks to u... – vasupradha Nov 22 '14 at 02:48
0
[self dismissViewControllerAnimated:YES completion:nil];

Write this in the animation completion handler or after you remove the view by this your code as you stated,

[view removeFromSuperview];
Rameswar Prasad
  • 1,281
  • 16
  • 34
  • I have tried this already, it says no visible interface for the declared. Though I have a view controller vc2, I have defined the subclass as a UIView than a UIViewController because I am performing some animations. – vasupradha Nov 17 '14 at 07:10