UIViewController within a UIViewController?

I recommend you, to use the following code.

Up vote 0 down vote favorite share g+ share fb share tw.

So I have a viewControllerA, and I want to add another View managed by viewControllerB to it. There is only one UISlider activating a simple action in viewControllerB. It won't crash if I don't touch this UISlider, it will once I use UISlider.

I am using ARC. I am using: self. View addSubView: viewControllerB.

View; to add viewControllerB to viewControllerA. Am I missing something? Thanks.

OK. It looks like a really simple situation. I just added one view controller and one action.

Here is the demo project code on github: github.com/randomor/Demo The reason why I want this to work is because I have another app that will create a view controller on the spot and add it to anther view. And I don't want to do it modally, because I don't want the new view controller to cover the whole screen. Thanks.

SOLUTION: So I'm now just using the latest ViewController containment API: self addChildViewController:viewControllerB; It works! As long as I added this line, the event will be passed to its own controller and it stopped crashing. Objective-c ios cocoa-touch link|improve this question edited 2 days ago asked Apr 6 at 16:07randomor1597 76% accept rate.

I recommend you, to use the following code in ViewControllerA. H #import "ViewControllerB. H" in ViewControllerA.

M (where you want to push the new controller) ViewControllerB *newController = ViewControllerB allocinit; self presentModalViewController:newController animated:YES; in ViewControllerB. M you will need self. PresentingViewController dismissModalViewControllerAnimated:YES; to make it vanish again.

Concerning multiple controllers for one open screen (Apple ViewController Programming Guide): "Each custom view controller object you create is responsible for managing exactly one screen’s worth of content. The one-to-one correspondence between a view controller and a screen is a very important consideration in the design of your application. You should not use multiple custom view controllers to manage different portions of the same screen.

Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.

I tried that, that doesn't work either. Weird. – randomor Apr 6 at 16:41 that has to work if you code it right ill update my answer – Sebastian Flückiger Apr 6 at 16:56 I updated the code - please try it out like that =) it has to work :) – Sebastian Flückiger Apr 6 at 17:01 ok, now I moved the code out from ViewDidLoad to another action triggered by a button, it does work now using the modal method.

But what I really want to achieve is not cover the whole screen with it. I want still be able to use the other parts of the parent view. – randomor Apr 6 at 17:12 then you will have to make the slider a subview of viewcontrollerA, but without having an own controllerB.

– Sebastian Flückiger Apr 6 at 17:29.

You should try and avoid the practice of nesting UIViewControllers. While it is technically supported in iOS5, it is ill-advised, for many reasons, including the type of problem that you're having (you have a dangling pointer to a UIViewController, which is why you are crashing). blog.carbonfive.com/2011/03/09/abusing-u....

1 that's a great link. Thanks! I'm also viewing the wwdc11 talk on view controller containment.

– randomor Apr 6 at 17:42.

Although this question is extremely vague, I imagine that you are not keeping a reference to View Controller B, and so when view B tries to interact with it, it causes EXC_BAD_ACCESS.

Thanks. How do you keep a reference to viewControllerB? I'm not doing an ivar.

I'm just creating it on the spot in ViewDidLoad and add it to the view. – randomor Apr 6 at 16:26 @randomor The way I've typically seen it done is you create a property on the parent view to hold a reference to the child view. I don't think this is your problem though because views retain their child views.

– mydogisbox Apr 6 at 16:41.

If it's a EXC_BAD_ADDRESS, then you may not be retaining the target, most probably the view controller for the slider.

The target for the slider is the viewControllerB cause it owns the slider. I'm basically alloc and init the viewControllerB and add it to the viewControllerA at ViewDidLoad. Yes, it is EXC_BAD_ADDRESS, but I'm using the ARC.

– randomor Apr 6 at 16:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions