Change a UIViewController to a UITableViewController inside a storyboard? - objective-c

I've made a view in my storyboard which I've now decided I'd rather display its data via static table cells.
I can't use static table views in a UIViewController (Static table views are only valid when embedded in UITableViewController instances). So, I need to convert my existing UIViewController to a UITableViewController somehow. I've changed the .h file's parent, but that hasn't done it.
Is there another way to get this going? I'd really rather not have to make a new VC in the storyboard and move everything over, it's a big hassle.

I'll add to this, since the question is about how to change a UIViewController into a UITableViewController, and given that this question is over a year old and the original answer, while valid and may or may not have been the only solution at the time, doesn't actually answer the question and is not the only solution.
It IS possible to do this, you just have to set up the table view delegate and datasource outlets in IB, and manually edit the storyboard XML, which sounds scary but is actually very easy.
First, change your class's parent to be a UITableViewController. UITableViewController already adopts the UITableViewDatasource and UITableViewDelegate protocols, so if your class does too you can remove them:
#implementation MyTableViewController : UITableViewController
...
#end
Next, create new referencing outlets on your UITableView for its dataSource and delegate. The easiest way to do this is to control-drag from the UITableView to itself. The popup will give you the dataSource and delegate options.
Lastly, you need to change the storyboard XML. The storyboard file can get pretty big pretty fast. The easiest way to find the scene you are looking for is by setting Storyboard Identifier in the Identity Inspector. To view the XML directly, right click on the storyboard file in the project navigator and select "Open As -> Source Code". Now just search for whatever you set the reuse identifier to earlier. You'll see something similar to this:
<!-- My Table View Controller -->
<scene sceneID="EuE-XX-cCb">
<objects>
<viewController storyboardIdentifier="MY_TABLE_VIEW_IDENTIFIER" ... >
// Lots of other stuff
</viewController>
</objects>
</scene>
All you need to do is change the opening and closing view controller tags
<viewController>
</viewController>
to be tableViewController instead
<tableViewController>
</tableViewController>
That's it! No need to create a new UITableViewController scene or embed a UITableViewController in a container view.
EDIT:
I should also add that the UITableView MUST be the root view. It cannot be embedded inside another UIView.

If you want your static cell table view not to take up the entire screen, then using a container view is the easiest way to go. Start with a regular UIViewController and drag a container view (next to normal UIView in the object list) into its view. Resize it however you want -- the storyboard will automatically provide a view controller connected to this container view with an embed segue. Delete that controller, drag out a table view controller and right-drag from the container view to this table view controller to make a new embed segue. This table view controller can be accessed from the UIViewController with its childViewControllers property (and conversely, you can access the UIViewController from the table view controller with parentViewController if you need to).

What I did, is creating a UITableViewController in IB, open the Storyboard with a text editor, and copy all the nodes inside from the UIViewController to the UITableViewController.
I think that with this way there's less risk of deleting something important.
Before copying the sections objects, make sure that both tableviews (UIViewController and UITableViewController) have the same properties set like: static or dynamic cells, style (plain or grouped), etc.

Related

Why I can't drag outlets from a custom UIView Class?

I have a TableViewController With a dynamic cell and a view I added above it.
I've created a UIView class and added it as a custom class for that view ( just like I do with a custom cell) but I can't drag labels etc from that UIView to its .h file. Only to the main TableViewController.h file. Any idea what's the problem?
A table view in a UITableViewController takes up the whole screen, so the view you added is actually in the table view, not above it, so that's why you can connect to the table view controller. If you want to add a view above the table, use a UIViewController instead, and resize the table view so it doesn't take up the whole screen.
I've noticed this too (for an OS X project) in Interface Builder. I'm not sure if it is a bug or not in IB.
What you will have to do is enter the #property declarations in your .h file manually and then you will be able to connect them.

Objective-C / iOS: Subclassing UITableViewController for a custom view

As we all know, table views in Cocoa Touch are one of the niftiest pieces of framework elements that's out there. As a convenience, Apple gave us a nice view controller class to encapsulate the functionality of a table view in a vc, the UITableViewController.
At the same time, there are times that we want to utilize the functionality of a table view without having it take up the whole screen. However, there seems to be no way to do this by subclassing UITableViewController. Instead, I had to hookup a table view and manually subscribe to the UITableViewDelegate and UITableViewDataSource. If I try to subclass UITableViewController, my app crashes before it can even put the view on-screen...
My question is, is there something I'm missing? When subclassing UITableViewController, I hook up my custom table view to the tableView property in UITableViewController. Is there something else I have to do?
UITableViewController only adds minor conveniences over UIViewController: it creates and positions the table view, hooks up the delegate & datasource (to itself, generally), passes the view controller editing property through to the table, and does a couple of useful UI bits when the view appears. (See [the docs][1] for details.)
Just about all of the above are either A) things that you're needing to change in order to have a non-fullscreen table, or B) things that you can do in a line or two each, and which UITableViewController only does for your convenience. For cases like this, you're better off using your own UIViewController subclass.
Step 1: Subclass UIViewController instead of UITableViewController
MyTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
Step 2: Use interface builder to drop a tableView and custom View
Step 3: Declare the tableView property as IBOutlet in your MyTableViewController header file and bind it to the tableView in the interface builder
IMHO, This process would give you more flexibility.

Replace the TableView in a SplitViewController

I am looking to replace the UITableView in the MasterViewController section of a UISplitViewController. Instead of the UITableView, I want just a View so I can place UIButtons, UILables, etc in it. These buttons would then control what is shown in the DetailView section of the SplitView.
I have asked this question one other place and someone suggested that I create a subclass of the UISplitViewController. This person didn't give much direction, besides retaining the DetailViewController.h, .m, and .xib and editing the MasterViewController.h, .m, and .xib to my liking.
Here are the steps that I have taken:
Created a new project and selected "Master-Detail Application"
Unchecked "Use Storyboard" so I could get at the xib files.
Opened "MasterViewController.xib"
Deleted the "Table View" under "Objects"
Added "View Controller" to "Objects"
Changed "#interface MasterViewController : UITableViewController" To "#interface MasterViewController : UIViewController" in "MasterViewController.h"
Commented out anything under "MasterViewController.m" that was causing problems because they were referencing properties of the TableView, which is no longer there.
Then I get this error: -[UIViewController _loadViewFromNibNamed:bundle:] loaded the "MasterViewController" nib but the view outlet was not set."
Am I on the right track? If I am, could someone help me out with the error I am getting?
Otherwise, if I am going at this wrong, could someone point me in the right direction?
Thanks!
now, with storyboard in the latest Xcode, here's what i have done …
create a new Master-Detail project, and allow storyboard
open MainStoryboard_iPad.storyboard
click on the TableViewController and then delete it
drag in a new plain ViewController
ctrl-click-drag from the navigation controller that is the master view controller to your new ViewController
remove the MasterViewController.h/.m files, and create a new class that is a subclass of UIViewController
take the name of your just created replacement class and place it in the Class name of the Custom Class section of your identity inspector
now, add buttons and wire up the way you want to. if you stick with a single view controller in the detail portion, you should be able to simply refer to the detailViewController directly and fill it as you want; for multiples, you can probably use replace segues.
for iPhone, the steps will be similar. you'll have to ctrl-click-drag from the nav controller to your new ViewController and set up the replacement ViewController.h/.m files in the same way. the segues from your buttons will be push segues, or you can simply call performSegueWithIdentifier:sender: .

Associating a UITableView with a TableViewController

Can anyone describe how it is possible to have a TableViewController with its xib file having a at its root and the uitableview as a subview?
I believe the TVController somehow assumes that UITableView will fill the entire area.
Why is that?
I have a really specific need to build a kind of side and bottom tabbed interface with a UITableView as its main area. Pressing tabs changes the predicate for the fetchedresultscontroller etc etc.
The reason I want this is because of the breadth and depth of the data categories in the data model. I rally need to flatten the menu structure a lot...other wise with a table and navbar controller structure, user experience will be akin to sinking to ever deeper depths of a cavern!
My idea is tried and true in other paradigms...in iOS it almost looks like it's Apple's way or the highway. I am OK with APPLE of course no gripe.
But I really would like to create this.
I tried adding a new tableviewcontroller complete with xib and then removing the UITableView in IB and replacing with a UIView and a UITableView as a subview, hooking up (I believe) the delegate to the file's owner.
I created an IV tableView for when I want to reference it and again used IB to hook it up in IB
Try to run it and it whines that...
[UITableViewController loadView] loaded the "TabbedTableController" nib but didn't get a UITableView.'
Really can't seem to get my head around what the issue is here.
There doesn't appear to be anymore I can do to hook the UITableView up!
Any help would be terrific. I'll send you a Christmas card in desperation :^)
Also...why should it be so and how strict is this UITableView fullscreen thing?
Enlighten me if you can. The docs I have read don't want to.
Regards
Keith
A UITableViewController does assume that the root view (i.e. the controller's view property) will be a UITableView, thus the table view fills the screen. When you need a view that combines UITableView with other top level views, you will need to do a little more work but it's not hard:
Your view controller will not subclass UITableView. Instead, do this:
#interface MyViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, weak) IBOutlet UITableView* tableView;
In Interface Builder, drop in a UITableView and whatever other controls you need. The table view can be any size and in any location in the view hierarchy. Also in Interface Builder, ctrl-drag from the table view to your VC and set the delegate and dataSource outlets, and ctrl-drag from your VC to the table view to set the tableView outlet.
Your view controller implementation should be the typical table view controller implementation: cellForRowAtIndexPath, etc.
A UITableViewController is more or less just all of the above work packaged up into a single unit for you.

Changing rootviews view type in an iOS app

While creating a navigation based application, it automatically creates a root view controller which subclasses UITableViewController, but in MainWindow.xib as you know I can't see a UITableView is placed under root view controller but we drag and drop a table view there. Can we simply drag and drop a UIView instead of a UITableView and change the root view controller class to sublass a uiview instead of UITableView and change its methods?
Or I must drag a UITableView in IB for the root view controller? I am a beginner and I do not want to make complicated things so what is the simplest way of using a UIView as a root. If thats not simple I will stick with the table view.
Yes you can change. You just need to change some bindings in inteface builder and change rootViewController's superclass to UIViewController instead of UITableViewController.
UPDATE
First open the rootViewController.xib and delete the tableiew from there and drag-n-drop a UIView. Bind that View to view property in file-owner. Change superclass of rootViewController from UITableViewController to UIViewController. That's it.
Here are the screenshots.