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.

4
  • You can remove the child view using [childview removeFromSupreview] Commented Mar 18, 2013 at 5:21
  • what happens if you put the child as the target instead of putting self? Commented Mar 18, 2013 at 5:24
  • change the addTarget: parameter to whichever view controller implements the method closeMe Commented Mar 18, 2013 at 5: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. Commented Mar 20, 2013 at 2:53

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

I didn't specify this in my original post, but I don't want a modal presentation.
0

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

Check this

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.