简体   繁体   中英

Invoking a UIView Controller from a UIView

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?

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.

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
[self dismissViewControllerAnimated:YES completion:nil];

在动画完成处理程序中编写此代码,或者在您以此代码删除视图后,

[view removeFromSuperview];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM