Where to write code of embed viewcontroller in containerView - ios

I am working on a project which contains 3 container view with 3 child viewcontrollers. I am new at container view So I want to know where to write code of that child view controllers. I load that viewcontrollers with adding child viewcontrollers in containerview with use of seague.
As per I googled, There are demos like they only load viewcontrollers in containerview with just changing background color.
I want to know that If I have to make different classes for that view controllers that embed in containerviews?
Thank you for your help

Yes you have to create UIViewController class for each embedded View Controller.
Doing this will give you control over the embedded View Controllers like normal View Controller do.

Related

Many to One Segue

I have a ProductDescription ViewController that gets called from a ProductTable UITableView that I have placed in many ViewControllers.
It doesn't seem very efficient to ctrl+drag a segue for each tableView in the Storyboard, as I have approx 20 of them.
How does one do this programmatically?
You have several options within UIKit to programmatically show a view controller without using a segue:
Push a view controller onto the navigation stack:
pushViewController:animated:
showViewController:sender:
Present a view controller modally:
presentViewController:animated:completion:
The real answer here is to use storyboard references. You shouldn't have the same thing in twenty different spots all trying to link to the same view controller to the point of asking this question.
So, let's create Product.storyboard, a storyboard which simply has two view controllers:
ProductTableViewController
ProductDescriptionViewController
And the appropriate segue between the two controllers.
Now, everywhere else in any of your other storyboards that want to use these controllers with this relationship, simply add a storyboard reference, add a container view controller, and add an embed segue between the container view and the appropriate view controller in the product storyboard.
You can accomplish this same effect even without using storyboard references. Ultimately, the main point is to use container views and make embed segues from everywhere you need this relationship to the first of these two controllers, and then there's just a single relationship created between the two product view controllers.

Multiple Views For One Controller in Swift

I have a controller called PageViewController. Now I have created several view controllers in my storyboard. Ideally I would like to connect all of the view controllers in my storyboard to PageViewController and then when PageViewController is loaded, PageViewController will choose which view to display to the user.
I don't want to create the view programmatically since I'll also have to create numerous layout constraints to each view and that will be time consuming. My questions is this, since I connected multiple view controllers to PageViewController, is there a way for me to tell my app which view controller from the storyboard I want displayed for PageViewController.
Not sure of what you want but you can get your controller from your storyboard using its identifier :
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("myControllerIdentifier") as! MyControllerClass
You can present it using presentViewController:

Is it bad practice to put UIViewControllers in other UIViewControllers?

I know there is the common practice in iOS development of having one UIViewController presented on the screen whose view is loaded from a XIB which will contain all the UIView subclasses in it.
While I was working on a pretty complex widget for an app, I decided to make the widget subclass a UIViewController instead of a UIView. This was because I thought a UIViewController was essentially a UIView with some helper methods around it. Then I could create a XIB for it (I know UIViews can have their own XIBs too), load the views it contains, place ITS view in the presented parent VC's view, and lay it out.
This works fine so far, but I'm wondering if this is bad practice and if I should just subclass a UIView instead and give it a normal NSObject controller. I am seeing some problems with this and I was wondering if anybody could address concerns I have with this approach?
EDIT NOTE: The widget VC does NOT relate to the VC view it is in and is reusable on ANY screen. So the answer is not subclassing the widget VC with the parent VC. The widget is INSIDE the parent VC, but it is NOT a parent VC.
EDIT NOTE 2: I am NOT using Storyboard. Only Autolayout, XIBs, and ARC.
Why can't we have VC's in VC's?
1) Can VC's be simply dropped into ANOTHER VC's XIB and be loaded easily as a subview?
2) I read here: When to use a UIView vs. a UIViewController on the iPhone?
The top answer explains how the VC controls rotation of the screen and laying out the subviews again, so if you add another VC, then the system will automatically think that THAT is the main VC and will attempt to rotate it instead, causing problems. Is this true? Or is he just talking about if you somehow got into a state where 2 VC's were "presented"? I wasn't sure if his answer applied to VC views that were SUBVIEWS of other VC views.
3) In general is this good practice? It certainly seemed more reasonable as it made loading the subview VC's view much easier.
Thanks!
It's absolutely fine. The answer to your problem is ContainerView.
Container View defines a region within a view controller's view subgraph that can include a child view controller. Create an embed segue from the container view to the child view controller in the storyboard.
You almost got it right. Yes, it's good to make a view controller for what you needed. But you shouldn't just add it's view to the parent view, you should also add the view controller as a child view controller of the first view.
You can add many views controllers as child view controllers of a view controller.
You can learn more about this here.

accessing 2 viewControllers

I am creating an ipad application, I two view controllers the size screen of an iphone 5 and I would like to show both of them on the ipad screen, as two distinct UIViewControllers though. Is there a way to do it?
I have tried to alloc the second viewcnotrller in the viewdidload of the first, but what I notice is that it alloc the first and the second, but the first is not accessible any more (it looks just like a still image).
You can do it very easily in the storyboard. Just add 2 container views (next to the regular UIViews in the object list) to your controller's view, and size them how you want. You will automatically get 2 view controllers connected to the container views by an embed segue. Just change the class of these 2 controllers to your custom classes, and you should be good to go. If you need to get a reference to these controllers from the main controller, you can get it from the childViewControllers property. Your main controller (assuming it's the initial controller) and the 2 child controllers will all be instantiated at start up with no code necessary.
Check out view controller containment, in which you have a container view controller, which then can load one or more children view controllers. Also see the relevant section in the View Controller Programming Guide. Also see the WWDC 2011 session, Implementing UIViewController Containment.

How to subview a UITableViewController within another Controller using Storyboards

I have encapsulated all my table view logic on a UITableViewController that is linked to a view. This was done using storyboards.
I would like to embed this logic and view within another view controller / view (kind of like a header information with a scrollable table beneath.)
I have the following components:
CustomViewController which is linked to a UIView (dragged in from storyboard)
CustomTableViewController which is linked to a UITableView (dragged in from storyboard)
Essentially I am trying to mimic the scenario of the Stopwatch in the iOS clock app
What is the beast approach to this?
How is it done programatically?
Can this be done on the storyboard somehow?
Any help would be greatly appreciated. Thanks
Ok figured it out. This solution is iOS 5 specific since this feature was added there. This method works with storyboards.
Setup: The intention is to host one view controllers view and logic within another controller.
Since there is no intrinsic way to reference the child view controller in the storyboard, we need to give the view controller a name. This can be done by filling out the "Identifier" attribute on the controller in the storyboard. NOTE: Make sure you are giving the controller the identifier and not the controllers view.
Instantiate the controller you want to aggregate. This can be done from the hosting controller.
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"ControllerIdentifier"];
Add the child controller to the parent controller
[self addChildViewController: controller];
Add the child controllers view to the parent controllers view. Note if you have a place holder view in the parent controller you wish to add the child view to, then this is where you do it. Here I add it the a UIView called stage in the parent controller.
[self clearStage];
[self.stageView addSubview:controller.view];
presentedController.view.frame = self.stageView.bounds;
And that is it. Pretty simple. I have used it successfully with switching controllers and view in a home made tab control. The sub controller enlists its views in the view lifecycle, so the viewDidLoad, etc all work as expected in this child view controller.
Hopes this helps someone.

Resources