How to set the controller for a subview in storyboard? - ios

I have a view in my storyboard to which I've added a UICollectionView. Is it possible (using Interface Builder only) to assign to my collection view a different controller than the controller that I use for the view that contains it?
I am using XCode 6.1.

You can add the object to yours main view controller, like in image:
then, set in class in it settings:
and set it like delegate and data source for yours collection view.

I was able to do it by removing the UICollectionView, and replacing it with a Container view. Then, I created a separate collection view controller, and linked it to the container view with an embed segue (ctrl-drag from the container view into the collection view controller, and choose embed).

Related

Should I embed a view controller's view within a new view?

When adding an external view controller to an existing one in the Storyboard you would use a "container view". When adding one programatically you are presented with the option of creating a new UIView to embed the external VC's view within (replicating a container view) or simply just adding the external VC's .view directly into the existing one.
Which is considered the better practise here when doing this programatically? Does one have a different effect than the other on performance or reliability?
Using a container view in Storyboard automates the process of:
instantiate view controller
add as childViewController
add its view as a subview of the container view (which is a subview of the main view)
Adding a child view controller via code is the same process:
instantiate view controller
add as childViewController
add its view as a subview of the current view, or as a subview of another subview of the current view
The other benefit of using a container view in Storyboard is that you get a visual design interface.
Of course, some people don't like Storyboard / IB, and prefer code-only approaches.
So really, whichever method best suits your needs and development style.

Swift : How do i make a master page structure in view controllers?

I am working in a project that it contains a header view. There is a UIImageView inside header view. I want to derive this header view and its appearance (constraints,bg color etc.) on all of my other view controllers. So is that possible to do it just using Xcode designer ?
I created a view controller in storyboard and BaseViewController class.
In second view controller, i created another class and derived it from BaseViewController. But this didn't work.
Create the view in xib the use that view in all viewControllers in all storyboards. For creating a custom view you can see this video:
https://www.youtube.com/watch?v=IrgH522lbfA

Swift3, How to use same UITableView in different viewController

In my project, I have table including many cell prototypes, outlets and constraints already in xViewController class.
I would like to know how if I want to use this table again in yViewController class.
Is it possible or I have to create the new one?
You can use ContainerView for the same. You can take a separate UITableViewController or UIViewController as per your requirement.Let called this class CommonTableViewController.
In your XViewController , remove tableview and move all code to CommonTableViewController. Drag and drop container view from the object library on XViewController. You can delete pre- defined child of your ContainerView. Now add embed segue between XViewController and CommonTableViewController.
Drag and drop container view on YViewController as well and add segue as mentioned above.
Do let me know if you have any further queries.
Refactor your table view controller to its own dedicated storyboard (In Interface Builder: Select the table view controller, and choose on the Menu bar: Editor > Refactor to Storyboard...).
Make sure your table view controller is the "Initial View Controller" (has an arrow pointint to it from the left, on the storyboard).
Now, on the main view of every view controller on which you wish to display your table view, insert a Container View and replace the target of the Embed segue with a Storyboard Reference to the Table View stotyboard you just created.

Getting a VC to appear inside another VC

I have 2 ViewControllers.
VC1 has a 2 views inside it.
I want to get MenuUIVC to appear in one of my views belonging(child) to VC1.
I tried this code inside of VC1 but it didn't work.
MenuUIVC * menuViewVC = [[MenuUIVC alloc] init];
menuUIView = menuViewVC.view;
I expected to be able to see the MenuUIVC inside the view (menuUIView) which is a child of VC1. I have the IBOutlets all hooked up on the storyboard.
You can use Container views to get View Controller inside a View Controller. And, you can get the reference to it through prepareForSegue() method.
Links for description:
iOS Container View
The Easy Way to Switch Container Views in iOS
If you are using UIView, then why don't you use
[menuUIView addSubview:menuViewVC.view];
Another option is to use childviewcontrollers.
See these links for example:-
adding view controller as child view in ios
Add child view controller to current view controller
Using container or child view controller, gives you full fledge option of using View Controller properties, which you will not get in UIView.
Say for example, you can you orientation delegate methods, which you can't using in UIView class directly.
Hope this can help you.
Cheers
Sanjay
you can go for container view if you want to do it using IBOutlets & storyborad.
but if you want to do it using code then follow the below steps :
initialise & create parent view controller.
then add required child view as a subview with negative frame.
now change the frame with UIView animation whenever required. & do add tap gesture recogniser on part of screen other than the child view to remove it back to the initial position.

Use a Collectionviewcontroller on a view inside main view

In my IPad Application I have a main view and it contains three child views
top
middle
and bottom
Is it possible to use different view controller on different child views?
For Example i want to use collection view controller on the bottom view and top view.
if yes then is it possible to add and delete cells dynamicially? A small example would be appreciated.
If you are using iOS 6 you can create a container view controller easily in a storyboard by dropping ContainerView from the object library.
Otherwise you will need to implement a container view controller manually. See the documentation.
To Use a collection View Controller on a view first you have to create a UICollectionViewController with its CollectionView somewhere else. you can create this CollectionViewController in interface builder or programatically. then treat the collectionView of the UICollectionViewController as normal View and add it to your View with desired frame.
If you are not familiar with CollectionView then take a look at some Tutorial on UICollectionView on Google
UICollectionView tutorial

Resources