Steven
Steven

Reputation: 1098

Adding UIView with its own UIViewController on top of another UIView

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.

Upvotes: 0

Views: 786

Answers (4)

nielsbot
nielsbot

Reputation: 16031

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.

Upvotes: 0

Anil Varghese
Anil Varghese

Reputation: 42977

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

Check this

Upvotes: 0

Adrian Sluyters
Adrian Sluyters

Reputation: 2251

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.

Upvotes: 0

KDeogharkar
KDeogharkar

Reputation: 10959

-->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.

Upvotes: 1

Related Questions