0

I want to bring up a UIView on top of another UIView. I want to do this in a way such that the child view will have its own UIViewController. Is this possible?

So I should be able to add a button to the child view:

UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
...
[closeButton addTarget:self action:@selector(closeMe) forControlEvents:UIControlEventTouchUpInside];

closeMe being a method in the child view's ViewController.

In my test code, when the user taps the close button, it crashes with an undefined selector because the parent view's ViewController has no closeMe method.

Edit:

Sorry, I mentioned the closeMe method only as an example. There are many other methods I need to support with this child view (e.g. handle view rotations), and my goal of the child view having its own ViewController is to encapsulate.

Steven
  • 1,049
  • 2
  • 14
  • 32
  • You can remove the child view using [childview removeFromSupreview] – Hasintha Janka Mar 18 '13 at 05:21
  • what happens if you put the child as the target instead of putting `self`? – Jsdodgers Mar 18 '13 at 05:24
  • change the addTarget: parameter to whichever view controller implements the method closeMe – Dos43 Mar 18 '13 at 05:24
  • OP UPDATE: found out why it was crashing. The selector was undefined simply because the child viewcontroller was a local variable in the method that created it. When the method exits, the child viewcontroller gets deallocated, hence selector fails. Moving the child viewcontroller to a strong ivar of the parent viewcontroller made this work. – Steven Mar 20 '13 at 02:53

4 Answers4

1

-->Make your ChildView With custom view that is the subclass of UIView.
-->Add this custom view as a child view in your view
-->And then use Protocol to achieve your task.

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • 1
    After further learning, I think there is no good way to do what I want to do. I think this answer is the best answer, so I'll mark it as correct. Thanks. – Steven Mar 19 '13 at 04:15
0

It wounds like you are trying to create a modalView?

Here's a straight forward example of modalViews: http://timneill.net/2010/09/modal-view-controller-example-part-1/

What I also like to do is pop all of my viewControllers in a NSMutableArray and make that a property (nonatomic, retain) so you can easily access them from pretty much anywhere in the app through hierarchal means.

Adrian Sluyters
  • 2,186
  • 1
  • 16
  • 21
0

I think your child view is a custom view, then you can use delegate/protocol. You dont want separate view controller.

Check this

Community
  • 1
  • 1
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
0

View controllers and views have a parallel hierarchy.. You can add view controllers as children of one another. Your parent view controller can load its child view controller and then install its child view controller's view in it's own view.

UIViewController  --→  UIView
       ↑                 ↑
       | parent vc       | superview
       |                 |
UIViewController  --→  UIView

But if you're just adding a subview, you may not need a child view controller.

nielsbot
  • 15,922
  • 4
  • 48
  • 73