I have several View Controllers, all using a UITableView, with identical custom cells. The UITableView Datasource methods are identical too (to the line); all that changes is the data source itself (the array from which the table view is loaded). It seems rather redundant for me to have to copy the exact same code for 5 such View Controllers (not to mention it's a bad coding practice!).
I therefore thought of creating a base (or parent) view controller that extends UIViewController, and have all my following (child) controllers inherit from this base view controller.
My problem is this: My base view controller needs to have a UITableView property, and it also contains the DataSource methods. I cannot get the UITableViews within the child view controllers to refer to the parent class for their DataSource.
Am I on a completely wrong track? Is there a better approach to this problem? Any help is greatly appreciated!
Just have one controller into which you add a property for the data source.
When you segue to the controller or create it set the data source property.
If all the code is identical except the data source array, you only need one controller.
In xcode add a new source file: e.g. MyDataViewerController which inherits UITableViewController.
In that file when created, add a property of the form:
#property NSArray *dataSource;
Click on one of your view controllers in storyboard and click the Class attribute inspector tab. Set the class to MyDataViewerController.
You can now use this controller to show any of your data. You can probably delete all the others.
I've assumed its an array containing the data. Replace this with whatever you use.
Assuming you segue to this controller from somewhere, set the property in the
prepareForSegue function.
Related
I have an ios app written in swift and there I have a UIViewController with a container on half of the screen. This container has embedded UITableViewController and I want to display there some data that I'm fetching from my webservice. The problem is that to fetch the data from web service I need an unique id number that I store in the UIViewController. So how can I pass that data from one controller to another?
I have a project on github (written in Objective-C) that does exactly what you want. It has the horribly unoriginal name "test".
It has a master view controller with 2 child view controllers, each of which is a subclass of UITableViewController set up to manage static table views. (That detail doesn't really matter. It's the method of setting up connections between parent and child view controllers that addresses your question.)
The idea is that you set up your child view controller in a container view and link it in with an embed segue.
When you use an embed segue then prepareForSegue fires when the views are loaded. In your prepareForSegue you save a reference to your child view controller(s) if you need to call it/them, and make yourself each child's delegate if you need it to call you.
The code would be different in Swift but the concept is identical.
In my project I have two view controllers, and I am having trouble connecting objects such as an UIImageView to the view controller. When I try to create the IBOutlet, it tells me that "Could not insert new outlet collection: could not find any information for the class named UIViewController". I believe this problem stems from the fact that my original declaration of my class is as follows:
class UIViewController: UIViewController {
when in fact the view controller is named mainScene instead. However, when I change the first UIViewController to what I think it should be (mainScene), it doesn't even show me the option of connecting an IBOutlet...
class mainScene: UIViewController {
So, I have two questions.
Do I need to have a whole separate class for the second UIViewController and would that solve my issues?
Is there a better way to link objects to the UIViewController or am I just doing something horribly wrong (the likely scenario)?
Thanks so much
Short answer: 1. Yes, and yes. 2. There's no better way, and you're not doing something horribly wrong. (You probably just missed a step.)
You have two view controllers. Assuming they are different, you would subclass each one from UIViewController with a different name. E.g., mainSceneViewController and otherSceneViewController.
Your mainSceneViewController and otherSceneViewController would each have their own properties and IBOutlets.
Where you're probably stuck, is needing to change the class of your viewController within Interface Builder to match the class name in its .swift file, so IB knows what outlets it can connect for that view controller.
Each scene in your storyboard corresponds to a view controller. When the segue is performed, iOS instantiates your view controller from the storyboard.
While it is possible to only have one view controller subclass, and use the same subclass for different views, it doesn't happen too often.
Update:
Subclassing lets you add properties and methods to a class, and override their superclass.
In your comment, UIViewController is the class, and mainSceneViewController is subclassed from UIViewController. For your second view controller, it would likely be class otherSceneViewController: UIViewController {, as your other scene would likely require a different properties and methods from your main scene view controller.
You can read more about Inheritance in the Swift Programming Language guide.
I have one view controller containing lots of operations. Most of the views are created dynamically. To separate out code I created a class as a myClass of NSObject where I created a function showMyTableView. This function adds a tableview as subview. but I have to write all tableview delegates inside view controller. How I can move require delegates to myclass?
Tomorrow I may require to add subview containing buttons for which I should write action in separate class.
How I can accomplish same?
your new class should handle data only, not ui.
for different table cellview consider use separate tableviewcell subclasses and provide them data models. let them configure themselves
(By 'table view delegates', I assume you mean your table view's delegate and dataSource?)
You can certainly move this stuff to another class. You would simply set the delegate/dataSource of your table view to an instance of MyClass, in your view controller, like so:
self.tableView.delegate = instanceOfMyClass;
MyClass probably shouldn't be responsible for creating the table view and adding it to your view controller's view though. Your view controller should be responsible for this itself.
I have a Table View Controller that displays information using JSON. I want to change the styling of my app, and I don't want it to have that "table" view that it has now. Whats the easiest way to change my Table View Controller to a regular View Controller, the biggest problem I have is that the code uses a tableView and I dont know how to get it to work as a regular view controller.
I using a Storyboard with a TableViewController thats linked to a controller called UpcomingReleasesViewController.
I want my app:
To look like this:
My original answer was assuming you just wanted to convert from a UITableViewController to a UIViewController. Looking at your screen snapshots, I infer you really want to switch from a UITableViewController to a UICollectionViewController (which is an iOS 6 feature than allows you to do precisely what you want).
In that case, change your base class to UICollectionViewController, and replace your UITableViewDataSource methods with UICollectionViewDataSource methods. And then redesign your scene using a Collection View Controller.
See the Collection View Programming Guide for more information. Or see WWDC 2012 sessions Introducing Collection Views or Advanced Collection Views and Building Custom Layouts.
If you need to support iOS versions prior to 6, then you have to do this collection view style coding yourself manually, putting your image views on a scroll view and using a standard UIViewController. It require more effort than using a collection view, but can be done.
Original answer:
If this view controller will have a table view on it, but you just want to add more controls to the scene? If so, just change the view controller's base class from UITableViewController to UIViewController, create the new scene, add a table view to it, and specify the table view's delegate and data source to be the view controller:
Also, make sure you define an IBOutlet for your table view (and if you call it tableView, that will minimize any coding changes needed).
If you do that, you can quickly convert a UITableViewController based controller to a UIViewController with minimal code changes.
If you're looking to make something like your new UI mockup, look into UICollectionView. You'll see many of the same concepts (i.e. dataSource, delegate method signatures are similar) that are used in UITableViews used in the collectionView API.
Sometimes I want to subclass UIViewController for some app wide customizations. Eg. something that all view controllers should perform during viewDidLoad or viewWillAppear or so.
Naturally I subclass UIViewController and go from there and all view controllers inherit from that. But some of the controllers run tables. And there is UITableViewController designed for that purpose.
So I subclass UITableViewController too and just do the same things there. Which does not seem to be the smartest thing in OOP terms. And there is no multiple inheritance etc.
And as UITableViewController inherits from UIViewController ...
Now I am asking myself why I don't just create my own table view controller that inherits from my very own view controller subclass and adds all the table stuff. But what is "all the table stuff"?
There is the skeleton code that xcode adds to every new table view controller. Quite handy but that can be easily moved to code snippets.
There is the declaration of the protocols UITableViewDelegate and UITableViewDataSource. Easy going. The implementation of those methods has to follow in each subclass of UITableViewController anyway.
There are probably reasonable default implementations of all those mandatory methods in the protocol. Such as returning 0 for numberOfSectionsInTableView: or nil for titleForHeaderInSection or 44.0f for heightForRowAtIndexPath: (Bad example though. Could be smarter not implementing that at all)
So despite the obvious stuff, are there any miracles that UITableViewController takes care of?
I believe all of the behavior UITableViewController adds is well defined in the class documentation: https://developer.apple.com/documentation/uikit/uitableviewcontroller?language=objc
The UITableViewController class creates a controller object that manages a table view. It implements the following behavior:
• If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.
• If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.
• When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.
• When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.
• It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.
All of these are behaviors which should be easy to re-implement if they apply to your specific controller and table view.
In general I have found it preferable to implement these behaviors myself in order to allow for alternate inheritance hierarchies as you noted and because I usually consider setting both the delagate and datasource of a table view to be a view controller a design smell. Those are independent concerns which usually can and should be handled by some other class (e.g. a dedicated data source for a particular model class) rather than bloating a view controller.
So despite the obvious stuff, are there any miracles that UITableViewController takes care of?
Not that I am aware of. As far as I know UITableViewController is mostly a convenience class that can be replaced with your own subclass that adds a few lines of code.
Apple's class documentation pretty much says all that UITableViewController does (and I'm not repeating that here because it may well change in the future). Sometimes, additional information can be gleaned from a class' header file, but in the case of UITableViewController.h the source code comments just repeat what is already in the class docs.
In the end you must decide yourself what you want to replicate in your own subclass. Maybe your project does not need .nib handling? Or none of your table views is editable? So just don't code that...
It seems that the UITableViewController takes care of a lot of management issues that really you could just do for yourself, if you wanted to. Check out the documentation -- it will automatically create a UITableView for you, reload it, etc.