How to add UITableView to UIView which is already a subview? - ios

I have specific situation for which I haven't found solution.
I'm doing over storyboard. So I have ViewController on which I'm using segment control for switching subviews. Every subview is ViewController but one of them is using table view. So basically it's like this:
HomeViewController
FavoritesViewController
Table View (with custom cell)
GalleryViewController
How to properly set up this? Should I rename/refunction FavoritesViewController to FavoritesTableViewController or?
If there is any other question, please ask it.
UPDATE 1:
FavoritesViewController is having just UIView in UI because I don't need whole UIViewController

Create an IBAction from the segmented control. Inside the handling method, use "ViewController containment" to switch between two view controllers.

Related

UIViews buttons inside a scrollview not triggering IBAction

I have a horizontal scrollview with pagination enabled so each page will show a distinct xib view. Some xib views have buttons in them that linked to IBAction. I declare the IBAction in each xib's class. Everything seems and looks good the buttons show clicking effects and all, but the IBAction isn't fired?
Here is a code sample:
let clockView: ClockViewController = ClockViewController(nibName: "ClockViewController", bundle: nil)
let carView: CarViewController = CarViewController(nibName: "CarViewController", bundle: nil)
scrollview.addSubview(clockView.view)
carView.view.frame.origin.x = self.view.frame.width
scrollview.addSubview(carView.view)
clockView and carView both have buttons in them and the buttons are linked to IBActions declared in ClockViewController.swift and CarViewController.swift respectively.
Here how one of the views look like. I'm not adding UIViewController in the xib, I'm only using a UIView. I'm guessing this could be part of the problem but I can't use UIViewController as subview because it gave an error.
Any help is appreciated,
The problem is that you are declaring carView as a local variable, so it will be released once the function exits as there is nothing to hold a strong reference. The view elements themselves are referenced by the scrollview, so they aren't released but there will no longer be a view controller instance to process the events.
If you change carView and clockView to be properties then a strong reference will be held.
The issue seems that you are using ViewControllers inside ViewControllers. If you'd like to do this via storyboard, you'll have to use a containerView and place the ViewController within this containerView using the embed segue that it comes with.
If you'd like to do this programmatically, you'll have to add child viewcontrollers to the parent viewcontroller that emcompasses these child View Controllers that you have which are carView and clockView.
P.S. it's a bit confusing that you are calling your view controller carView, perhaps CarViewController is less confusing :)

Convert TableViewController to ViewController

I have started with a TableViewController but now I would like to change from a TableViewController to a ViewController in the storyboard. Is there an easy way to do this? I want to avoid creating a new View scene in the storyboard and copying the code over.
You have to delete the current TableViewController and create a new ViewController.
If you will set an TableView inside, you also must set DataSource/Delegate, and conform with the Table Views protocols in the ViewController Class.
You can do that using ContainerView. Take one UIViewController and add a ContainerView inside it. Control drag from the ContainerView to TableViewController and select EmbedIn. This is the only way to keep existing TableViewController, but you need to move your code from TableViewController to ViewController. Do this only if it is necessary to use TableViewController and you have to display a View outside the TableView.
Just start implementing ViewController protocol and remove TableViewController, TableViewControllerDelegate and TableViewControllerDataSource.
Also remove the methods from the TableViewDataSource and that's it.

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.

Choose subview with UISegmentedControl

I have an iOS application with a subview, as well as a UISegmentedControl. I want to use my UISC to change subview1 to subview2. I decided to drag out a few more ViewControllers onto my storyboard, and put subview1 into the first, and subview2 in the second. After connecting the ViewControllers and making outlets for the subviews, I went back to my main ViewController (at this point there are three) and did
#import "SecondViewController"
SecondViewController *myView;
self.mySubview = myView.myOtherSubview;
but the subview on-screen didn't change. Is my template method of doing this totally wrong? If so, how would you suggest I proceed?
Look at [UIView transitionFromView:toView...]
The basic idea is you'll have to child view controllers (check the docs in UIViewController under the child view controllers section) and transition from the view of one to the view of the other.
https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/clm/UIView/transitionFromView:toView:duration:options:completion:
This question, IOS Storyboard: Load Multiple Subviews for given position, is the same as mine, phrased better. The answer given there worked.

Resources