I am developing an app in which a UIViewController (firstViewController) contains some UILabels, a UIButton, and a UIView (subView). The UIView should display the UIViewController (secondViewController) that contains some layers. I am unable to do this.
What should I do to display secondViewController within subView of firstViewController?
You should use UIViewController containment or parent/child view controllers. You can read details here.
The most basic version is:
UIViewController *parentVC = ...
UIViewController *otherVC = ... // it's view will be added as subview
[parentVC addChildViewController:otherVC];
[parentVC.containerView addSubview:otherVC.view]; // containerView is a view where your child view controller should go
[otherVC didMoveToParentViewController:parentVC];
If you only add other view controller's view as a subview, child view controller won't receive all the events. For example, if you use other methods suggested here (just add view as subview and nothing more), you won't get -viewDidAppear: message (and others) sent to your child view controller.
You can do that by adding view of another view controller as sub view in view as bellow
SecondVC *aObjSecondVC = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondVC"];
[self.view addSubview:aObjSecondVC.view]
You can add it using the following line:
[self.subView addSubView:secondViewController.view];
Related
I have a custom UIView which is a xib file and then a class that controls this view. I also have a storyboard which a lot of different view controllers inside. How do I perform a segue from the UIView to a specific UIViewController that is inside the storyboard?
Thanks
You can't do that. What you can do is give the UIViewController a storyboard ID, using the menu on the right of the interface builder.
Then call it programatically, like so:
MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[self.navigationController pushViewController:vc animated:YES];
You could put a ViewController into the Storyboard, set the class to your custom ViewController. Then, if you have class files for the view in the xib, set your custom view as the VC's view (already saves some code in loadView) and then just add a segue from the custom VC to the other view controller. To trigger that segue, you have to call [self performSegueWithIdentifier:#"identifierOfSegue"] in your custom ViewController.
I have one viewController and assigned a viewcontroller class in storyboard. I drag drop another UIView on the existing view controller. How to assign a different objective C or UIViewController class to that inner UIView?
As i understand your question is ,
you can assign new view to your viewController using
viewController.view=innerView
or if you want to add another viewController to current view you can use (Implementing UIViewController Contentment)
[self addChildViewController:newVC];
[newVC didMoveToParentViewController:self];
newVC.view.frame = CGRectMake(0,0,160,504);
[self.view addSubview:newVC.view];
I have created a special class of UIView that has certain properties, and I did so programmatically because it is usually blank but will at times contain other views. I know that if I create a UIView programmatically I can do something like [specialView addSubview: aView];
My problem is that there is a UIView that I created in storyboard and I need to add that UIView as a subview on my special view. I have connected it as a property in my ViewController but when I do the same code as above, nothing happens. Thoughts?
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"MyScene"];
[specialView addSubview:myViewController.theViewToAdd];
And don't forget to give such an identifier to your scene (view controller) in Interface Builder.
Another solution is you can add the viewcontroller as a childviewcontroller.
func displayContentController(content: UIViewController) {
addChildViewController(content)
self.view.addSubview(content.view)
content.didMoveToParentViewController(self)
}
To remove :
func hideContentController(content: UIViewController) {
content.willMoveToParentViewController(nil)
content.view.removeFromSuperview()
content.removeFromParentViewController()
}
That UIViewController which houses the view you want may not be instantiated. So you would need to instantiate that controller and grab the view from there.
I would like to ask what is the correct way to add and remove UIViewController's view as a child view.
So, having UIViewController initialized I can add its view to view hierarchy as follows:
UIViewController *myViewControler = [[UIViewController alloc] init];
[someAnotherView addSubview:myViewController.view];
Question 1: Should I release myViewController.view after addSubview: call?
If I want to remove myViewController's view from view hierarchy I call [myViewController.view removeFromSuperview];
Question 2: How should I release myViewController instance in this case after its view removedFromSuperview?
You do not need to release the view, the owning view controller will do this for you.
I normally put the declaration of myViewController in the header and then release and nil it when I am done with it (either somewhere in the normal flow or in the dealloc of the containing view controller).
Mine is a normal viewcontroller application. When I click a button on this mainView, it adds a 2nd view(a different xib) as a subview to the mainView. I want this 2nd view to be replaced by a splitview. So it means I should replace the 2nd view controller with a UISplitViewController, isnt it?
Can I do that? Is it possible to add a splitviewcontroller's view like v add a viewcontroller's view?
You should be aware that, currently, the only official usage of UISplitViewController is as a root view controller of an application; Apple does not intend for it to be a child view controller. Apparently, this is due to bugs with handling rotation of the iPad, and may get fixed at a later date. So you should avoid adding a UISplitViewController's view as a subview to anything.
You can subclass UIViewController and then in the init:
UIViewController *left = ...;
UIViewController *right = ...;
UISplitViewController *splitVC = ...;
splitVC.viewControllers = [NSArray arrayWithObjects:left,right,nil];
self.view = splitVC.view;
return self;
Then just use this as a normal UIViewController.