UITableViewController inside a UIViewController - ios

I'm new to iOS and objective-C and I'm having some trouble in understanding how controllers work.
In my code I have a UIViewController (with my custom controller assigned by storyboard) and inside it, together with other objects, I want to have a table handled by a different controller. What is the right way to do this?

You can make that table view controller a child view controller of your UIViewController.
In the storyboard, you can do this easily by dragging a container view into your controller's view, and that will give you a child view controller automatically.
You'll want to:
delete the child view controller it gives you (it's just a UIViewController)
drag out a table view controller
control drag from the container view to the table view controller
choose "embed".
If you need to get a reference to this table view controller from the UIViewController, you can do that in prepareForSegue -- the table view controller will be the segue's destination view controller, and prepareForSegue will be called right after the controllers are instantiated.

You'll want to use an embedded container view.
Drag a "Container View" from Interface Builder sidebar into the view. This adds and links a default "contained" UIView/Controller as well.
Delete the entire UIViewController and View that was automatically added and linked to the container view (as you'll want a Table View Controller instead).
Drag a UITableViewController onto the Storyboard canvas.
Control-Drag from the Container View to the Table View Controller. Select "Embed" to contain the UITableView within the container view.
You're left with the parent view, now containing a UITableView via a Container View. The Controller for the Table View is on the storyboard canvas as well.

Add UITableViewController to storyboard, And create subclass (new file) of UITableViewController. In Storyboard go to Identity Inspector and in Class field type name of the subclass you created. After that you have to add your app logic based on your requirements.

Create Another UITableViewController in the storyboard, go to its inspector and assign it the same UITableViewController class that you have created before...

Related

Xcode Storyboard: Can't add View to ViewController

I'm using Xcode 10 (with Swift 5 and for iOS 12) and my app is currently set up like this:
UIViewController 1 (login screen) - segue12 to ->
NavigationController that automatically created
UIViewController 2 with a UITableView - segue23 to ->
UIViewController 3 (detailed info about an item in the UITableView with automatically created "back" button)
The NavigationBar only contains a " ".
I now want to add another UIView with a button below the UITableView but Xcode won't let me drag it below the UITableView, only either into the UITableView or directly underneath "First Responder", which puts the View outside into its own window.
If I delete "My Table View", it won't let me drag in a replacement either.
How do I fix this, so I can add the new UIView + UIButton?
NavigationController that automatically created UIViewController 2
That explains everything. When you drag a navigation controller into the storyboard, what you get is a navigation controller and its root view controller, and the root view controller is a UITableViewController.
Let's step back for a moment. A view controller can and must have exactly one main view. It occupies the whole scene. It cannot be resized. It cannot have a sibling view, because it has no superview for a sibling view to be a child of. It can only have children (subviews).
Well, in this case, ViewController2 is a UITableViewController, and MyTableView is ViewController2’s main view. (A table view controller is always configured this way, and that is what you got when you dragged the navigation controller into the storyboard.) That is why you cannot add more views to it, except as subviews, e.g. prototype cell contents. Your button has no place to go except inside the table view.
So, what to do?
If the extra interface you want to add is just a button, the usual solution is to put it into the navigation bar. It becomes part of the table view controller's navigation item.
If you don't want to do that — that is, if you really want the button and the table view to be siblings of one another in the interface — then you need to replace ViewController2 itself with an ordinary view controller. Now its main view will be an ordinary view, and you can drag anything you like into it, including a table view and a button. More likely, instead of a bare table view, you would use a container view, with an embed segue to a table view controller.

referencing outlets with new view controller table view

I've an button in my storyboard's main view. now I need to open an new table view in new screen when this button will be clicked. For this, I've
1. dragged an new view controller in the empty space on storyboard.
2. dragged an table view inside this new viewcontroller.
now not able to define this new table view in ViewController.swift. ctrl + drag from table view to ViewController.swift not opening any dialog.
any idea what wrong am I doing here.....
From the details you have given, it looks like you have not set your viewController Class. Create a custom view controller class. Specify this class as the custom class in the Attributes inspector for the viewController scene and try adding your tableView.

Putting a navigation controller with hierarchical table views inside of a view controller in swift

I am making an iPad application which requires the use of a "drill down" style set of table views for users to add elements to a list (i.e. a user taps on a table view cell and it brings up more detail or options, like you would see in settings). My problem is that I need to be able to place this table view inside of a view controller amongst other content. I can add a table view, but I need the navigation bar at the top of the view in order to mimic a tableView inside of a navigation controller.
You need to use a UIViewController and
give it a tableView property
make it conform to the UITableViewDataSource protocol
make it conform to the UITableViewDelegate protocol
Navigation controller works exactly the same way regardless if you use a UITableViewController or UIViewController. Create storyboard segues and inform the detail view about the object to be displayed in prepareForSegue.

ContainerView nextViewController is not child stacked to container view as subview

I have 3 View Contorller
First View controller have Container View of height 300.0f at the center.
It has one embedded view Controller which is table View controller.
On cell selection it should navigate to detailsViewController.
All the process is ok.
But detailsViewController is not behaving as embedded view controller of containerView and not of same size as container view.
It takes whole screen size.
As it is triggered from the embedded View Controller it should follow that frame without overlapping other controls which are in First View Controller.
You need to embed not view controller with table view, but embed navigationController(you can hide navigation panel on up side), end set yours table view controller as root for it, and use pushViewController to go to detail page.
hope ganesh this will help you
Look You have a Container View inside it you have a tableView and By clicking an cell you pushing another viewController that what i concluded after reading your question(hope you done everything in modular way).
Now you must have UIView subclass(i.e separately creating a view class ) inside which you have a tableView so you pushing by
[self.nav pushViewController:sos animated:YES];
there are two way to push ViewController inside a view first by Callback(through blocks) or passing Navigation ref. in UIView Class .So you are pushing the new Controller over the navigation thats why it showing this behavior and which is obvious.

iOS: Keeping UIToolBar on top during segue

I have a UIViewController which segues (push) to another UIViewController. The first controller contains a nicely laid out UIToolBar menu and I want this to persist over the secondary UIViewController (and another other ones I push on the navigation stack).
Is this possible?
This can be done with a container view in a storyboard, or using the custom container view controller api in code. In the storyboard, you can add your tool bar to a view controller, drag in a container view, and size it to take up the rest of the view. You will automatically get a view controller connected with an embed segue to the container view. Select it, and embed it in a navigation controller. You can use prepareForSegue (which will be called immediately after your main view controller - the one with the container view - is instantiated) to get a reference to the navigation controller (it will be the destinationViewController).

Resources