Add a subview to a custom class UIViewController that loads from nib, in Storyboard - ios

To clarify on this ridiculous title:
I'm using my own subclass of JSQMessagesViewController (https://github.com/jessesquires/JSQMessagesViewController) - let's call it ACMessagesViewController. I'm loading my ACMessagesViewController through storyboard, by setting the storyboard UIViewController's custom class to ACMessagesViewController. I am trying to add a subview to ACMessagesViewController's view through the storyboard.
JSQMessagesViewController loads from a nib. When I add a subview to ACMessagesViewController's view through storyboard, the view disappears. The subview seems to be adding to a view other than my ACMessagesViewController's self.view, and by the time the viewdidload scope runs through, the subview is gone.
My questions are:
1. What is the proper way to add a subview (through storyboard) to a custom class UIViewController, when the UIViewController loads from a nib?
2. What is happening in this view loading process? Why is my subview (added through storyboard) adding onto a view that is not the same self.view in my ACMessagesViewController?
Thanks in advance!

I think it's one of those "if you need to work around it, you're probably doing it wrong" scenarios you so often hit in iOS development. :-) Really, if you want the functionality the JSQMessagesViewController provides, you shouldn't need to add additional views. But if you must add views, the "right" way to do it is to add them as subviews of whatever self.view is after you call super in viewDidLoad. If you don't want to construct views programmatically, you can put them in a separate nib (instead of the storyboard) and manually load them.
(Note: this is assuming viewDidLoad is getting called. If it isn't, you might need to hook into some other method.)

I used #Anna's suggestion to add the view in viewDidLoad but you don't need to use a second Xib or design the view in code.
What you need is:
A. Design your view in the your JSQMessagesViewController scene in the storyboard/xib file.
B. Create an outlet of the view you need to present.
C. In viewDidLoad add your view with view.addSubview(myView) (or [self.view addSubview:self.myView] for all the objc ones out there)
D. That's it!

Related

How to create a separate view in Storyboard to be included programmatically in UITableView?

I have a UIViewController with a UITableView that is fed with data from the local database. When the user first launches the app (after installing) the table view is empty and I display a UIView in the middle of the table view that contains a UIImage, a UILabel and a UIButton (a call to action).
The first version of this view I built programmatically, which was no good to me because every time I tweaked something I had to build the app again. Then I switched to the storyboard but had to drag a UIView to the middle of my tableView. It is working now but I don't like the way it is, I can't edit my table view cells without having to move the UIView out of the table view.
I'd like to have a way to build this view entirely separated from my tableView (or even from my view controller in question) and then reference it in the viewDidLoad call of my view controller.
Unfortunately Xcode does not allow us to drag views directly to the storyboard so I'm pretty lost here.
Please tell me if I haven't been clear enough. I appreciate any help you could give me.
UPDATE: It'd be particularly awesome to me if I could also create a custom Swift class for this view of mine and reference it in the storyboard. And then in my viewDidLoad I could simply instantiate my custom view.
Thanks in advance.
Create a XIB file in which you can drag a view (without a view controller).
In your code you can load the XIB using NSBundle.mainBundle().loadNibNamed("MyXibName", owner:self, options:nil).
In the XIB file you can give the UIView a custom class (like you can do with view controllers in storyboard).
You then of course have to retrieve the view from the array returned by loadNibNamed and cast it to your custom class.

How to add a UIViewController's view as a subview to a UITableViewCell?

I have a reusable UIViewController subclass (an audio/video player, let's call it MediaController). It works ok when I add it to some other view controller as a child view controller, however my requirement is to also add it in a UITableViewCell subclass. Since -addChildViewController: is a method of UIViewController, I'm adding my view to the cell like that:
self.mediaController.view.frame = self.containerView.bounds;
[self.containerView addSubview:self.mediaController.view];
(containerView is just a placeholder view in the cell's view hierarchy).
However, this causes problems, first because MediaController is having some logic in -viewWillAppear and -viewWillDisappear (that of course never get called) and second because it seems that autolayout does not work properly when MediaController's view is added to the cell.
Do you think I have some other option (maybe use the UITableViewController that owns the cell as a container?) or I will not be able to use this MediaController at all?
This question is the most relevant when I search, but it still doesn't solve my problem.
Thanks!
One thing I might try if it's possible is to have a UITableViewController that has static cells.
If you're using a UIStoryboard drag and drop a UITableViewController and change the content to Static Cells then in the cell you want to have your MediaController drop a Container View into that cell. Then drag and drop from that Container View to your MediaController and setup an embed segue.
The appropriate viewLifecycle methods should be called when displaying.
Here is the UIStoryboard setup
The other answer from aahrens did not work for me since I have a complicated table view and I was not using storyboards from the beginning.
What I ended up doing was to pass a weak reference of the UIViewController to the cell, so that I can make the "normal" call to -addChildViewController:. Ugly, but works fine.

how to place a UIViewController in another UIViewController through nib in xcode

I have developed a DetailViewController, derived from UIViewController. Now I want to use this DetailViewController inside another InfoViewController's layout. I found one way to do this by using addSubView method. But what I am looking for is to plot it through nib file, like other UIViews. I want to plot DetailViewContoller inside InfoViewController's layout through nib file. Is this possible?
Any response will be thankful.
Yes it is possible..
First remove the referencing outlet for view in DetailViewController.xib.
And in InfoViewController.xib drag-drop Object and edit the custom class to DetailViewController.
Now drag-drop view in InfoViewController.xib and add what ever subviews you want to it. Now add new view referencing outlet to DetailViewController which is under objects.
Hope this will help you.

iOS VIewController for UIScrollViews content

I have a UIScrollView in my app and I am adding some custom views from xib to it so you can horizontally scroll (tabbing) in ScrollView to change which one is shown. For now this works but I have a problem with connecting views to controllers.
I don't know how to choose structure of ViewControllers (how many controllers should I use, use nested controllers,...).
I have a rootView and its controller. In this rootView there is a ScrollView and this ScrollView contains some custom views (subviews) loaded from xib (using loadNibNamed method).
My question is should I use the same ViewController as for rootView also for these subviews in ScrollView? Problem is that the ViewControllers view property is already bind to the rootView (super view in rootView) so when I bind this view property also to subviews an error is occurred. Also if I create new controller for these subviews an error is occurred as well.
When I am loading subviews to the ScrollView with loadNibNamed method in ViewController of rootView, owner of these subviews is ViewController (owner argument of loadNibNamed method is set to self).
Can you tell me please, how should I solve this? What controller should I use for subviews, should I create new one or should I use existing one. Or should I use some nested controller? I am newbie in iOS development so I have a chaos in using ViewControllers right now...
If there isn't much code that is relative to controlling the sub views you could use just the root view controller. i.e A single controller for a single scene would be a good MVC approach.
If you are using it this way , don't change the view property of view controller as this messes it up for the root view - controller setup. If you just need a reference to this views you already have it with the return value of loadNibNamed. Also if you are setting the owner to self then create additional instance variable to hold the sub views(and not the view property) so that you can specify the owner from the xib itself and connect the references appropriately.
However if you have substantial business logic to be written regarding the sub views then its fine to create separate view-controllers(a single class would be fine if all the subviews behave the more or less same way if you are getting what i mean) for it. In the xib for the subviews, you can specify this class as the owner and when using loadNibNamed: you should create an object of the subviewcontroller class and specify this as the owner. This way you can modularize the whole thing.

Custom views with Storyboard

In complex screens (View Controllers) I used to separate the whole thing in smaller pieces (I call them widgets). These widgets consist basically of a MyWidget.h and a MyWidget.m file as well as a MyWidget.xib file, where the root element is a UIView and the MyWidget class is the File Owner of the UIView. In the init of this widget I do a loadNibNamed.
In my View Controller I then do a [[MyWidget alloc] init], which I add to View's Controller main view as a sub view. This, so far, works perfectly.
I'm now wondering, how to do the same with storyboard, because I cannot really start to drag in a UIView somewhere, I always have to start with an UIViewController, which I don't want to.
If there is no possible way doing this with a Storyboard, can I simply do it the old way, by using the Storyboard for my main screens and segues, and use a separate .xib file to define custom views?
Putting the widget/view in a separate .xib file works, and is appropriate especially if you might want to reference that same view from multiple View Controllers.
However, sometimes you do want to see the additional view/widget within the same storyboard, and it is possible. Here's how you do it:
Select your view controller in IB (click on the black bar below the view), then drag a UIView from the Object Library into the black bar:
When a view is in the black bar, it's instantiated like any other view in IB but just isn't added to your view hierarchy until you do so in code. Change the view's class to match your own subclass if necessary:
You can hook it up to your view controller like you would hook up any other view:
The added view shows up in your Document Outline and you can hook up actions and references there too:
Now, the problem that remains is that you can't actually see the view no matter how many times you try to click or double click, which would defeat the whole purpose of putting it in the same storyboard. Fortunately there are two workarounds that I know of.
The first workaround is to drag the view from the black bar back into your view controller's view, edit it, then drag it back into the black bar once you're done. This is troublesome but reliable.
The other workaround is more finicky, but I prefer it because it lets me see all my views at the same time:
Drag a UITableView from the Object Library into your newly added view.
Then drag a UITableViewCell into that UITableView.
Once you do that, your view pops out magically by the side, but you have a UITableView that you don't want. You can either resize that to 0x0, or you can delete it and your UIView will (usually) still stay visible.
Occasionally the secondary view will become hidden again in IB. You can repeat the above steps if you deleted the UITableView, or if the UITableView is still in the hierarchy you just need to click on the UITableViewCell and the view will appear again.
The second method works for UIViews but not so well for UIToolbars and is impossible for UIButtons, so the cleanest solution I've found when you need to include lots of different subviews is to attach a single secondary UIView to your view controller as a container that never gets shown, put all your secondary views in there, and use the UITableViewCell trick to make everything visible. I resize my dummy UITableView to 0x0 to make that invisible. Here's a screenshot of how it all looks like together:
If you're just looking to make your view controllers else-where(and not in your story-board), then there's a pretty simple way to accomplish this:
1) Create your CustomViewControllers(abcdController in the code I tried) with their individual xibs as usual.
2) Add a UIViewController(or whatever was the superclass of your CustomViewController) to the story-board.
3) Set the CustomClass to CustomViewController instead of UIViewController as shown here:
4) Finally, in your viewDidLoad, load the custom xib and you're done.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSBundle mainBundle] loadNibNamed:#"abcdController" owner:self options:nil];
// Do any additional setup after loading the view from its nib.
}
I think you can do something like this to get instance of specific viewcontroller from Storyboard and use view on top of it.
ex:
MyViewController* myViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"myViewController"];
UIView* view = myViewController.view; //Get the view from your StoryBoard.
Hope this helps
Thanks
Vijay

Resources