TableViewController and Tab Views (swift) - ios

I currently have a basic app where there's a list of items in a TableView and I want to adapt a tab bar underneath the TableView, where all it would do it load a different set of data onto the table. Same UI.
What would be the best way to handle this? Only thing I can think of is to create another separate controller and copy and paste the most of the code in here and direct the TabBarController to that controller.
Is there a more efficient way of doing this, such as somehow creating an adapter or checking which tab is selected and then listing the data?
Sorry if I dont make any sense. Thanks in advance.

You need to add child controllers(for tabs) into a base controller and replace the view of base-controller from selected tab child view-controller
This link may help you to make a custom tabbar:
https://github.com/hollance/MHTabBarController

Start a new Xcode project and pick Tabbed Application as your template. That will give you your tabs and two empty view controllers. You can either add a UITableView to those existing controllers or you can delete them both and add as many Table View Controllers as you'll need. To attach a new controller to an existing Tab Bar Controller, you do a cntl-drag from the Tab Bar Controller to each of the new controllers. Then pick "view controllers" as the relationship.
You can use the same view controller class for your Table View Controllers. To differentiate the views, use the "tag" field on the View object on each Table View Controller - Give each one a different number. Then in the generic Table View Controller code you can access that number as view.tag().

Related

Can I use one view controller to handle multiple views in a tab bar controller

I am making an app with a tab bar which will display three different timers. The three timers are almost identical aside from the time and a few labels. Currently I am using 3 view controllers on my story board for the timers which use the same class, and embedding these into a tab bar view controller. Is there a way I can have one view controller and then programmatically use it three times in the tab bar view controller? Is this even a good path to go down?
A view can only have one parent so you need three separate view instances to populate the three tabs. And since a view controller only has one view, each of the view instances in the three tabs needs its own view controller instance.
You can have one view controller class with three instances of that view controller, but you can't use one view controller instance and its one view instance to populate three tabs.

using my ViewController as a TabBarController

So i currently have a ViewController that has a tableView in it with a tab bar on the bottom. Its basically an instagram clone. However, i built all the tableView functionality and am now just getting around to playing with the tab bar items to segue to new view controllers.
I cant figure out how to attach my already created tab bar(which is inside a regular view controller) to other view controllers in IB.
The ctrl+drag from the newly created view controllers to my MAIN view controller doesn't work. It doesn't provide an option to create a view controller segue.
Below is an image of my current view controller and a brand new one to the right
Any help on how i can do this would be greatly appreciated
On you tabbbar controller ctrl+click and drag to the view controller you want to create a relationship to.
Then when the little menu appears, under 'relationship segue' click 'view controllers'
You should then end up with something like this. Continue the same process to add more relationships.

How to point two UINavigationControllers to one view controller using storyboards?

I am attempting to connect two UINavigationControllers, representing different tabs in a tab bar application, towards a single UICollectionViewController. The collection view controller will be populated by different data based on which navigation controller is the parent. When both navigation controllers are connected, navigating to the second tab displays a black screen and the viewWillAppear() code in the controller for the collection view does not fire. When navigating to the first tab, everything fires and is displayed correctly with data populated.
The storyboard setup looks like this:
The navigation controllers are connected to the collection view controller as seen here:
If I disconnect either one of the two navigation controller connections, the remaining connection to the collection view works and collection items are displayed as expected.
Is this the correct way to reuse the collection view? Is there something missing from how I am connecting the two navigation controllers with segues to the single UICollectionView?
I see this is almost a year old, so I'm not sure if you found an answer or not, but I thought I'd help answer if needed. Without knowing too much of your project, can I ask why you want a tabBarController to point both of its view controllers to the same one? Makes me wonder why tabBarController is necessary.
Setting up a test project, I witnessed the same you did. The only thing I can think of is that when a view controller is instantiated via the Storyboard, it creates an instance of it and is stored in the tabBarController's viewControllers[i] property, which the other tab may not have access to in the hierarchy. It would be best to just copy the view controller object from the Storyboard and paste it onto another area of the storyboard, and have the tabBarController instantiate a new instance of the same view controller object. That way, in your viewDidLoad method, you can tell what the selectedIndex is, and know how to load data, alter interface, etc. Copying/pasting the Storyboard view controller should keep intact any outlets/actions you've set up.
Hope that helps...

IPad Split View Implement in Another View

I am creating a iPad App and it has several views to load data,but for one view i need to add split view. I dont need split views in other views. They are just detail pages. I search Through the net and found lots of tutorials based on iPad split view. But the problem is they all are creating a project as Split view project or they create a window base app and add slipt view to the delegate. I dont need to do that, I need to implement this split view only for one view. Is There any way to overcome this problem?
You can add the split view inside a Navigation Controller.
Even if the Split View is a container view controller and Apple recommends in the documentation that all containers should not be embedded in other containers, adding a split view inside a navigation controller works correctly and I never noticed any side effect in doing it.
Basically what you should do is:
- in the app delegate create a UINavigationController and use it as root view of your application window
- hide the navigation controller navigation bar if you don't want to see it (showing a split view with a main navbar on top is not nice looking...)
- then add your view controllers inside the navigation bar.
Example: imagine you have this application views sequence:
FIRST VIEW (full view = detail page)
SECOND VIEW (split view)
THIRD VIEW (full = detail page)
So you can represent FIRST and THIRD as standard view controllers (full screen), while SECOND will be a split view. Your app will be initialized by creating the main navigation controller, adding FIRST on it as top controller and using the main navigation controller as window's root view.
Than use the navigation controller push, pop methods to switch between these views or change the navigation controller "viewControllers" array directly if you don't want the recommended push/pop methods.
If you need to add special behavior to the navigation controller based on the type of view on top, just register your app delegate as navigation controller delegate (or a "main controller" object dedicated to this if you don't want to complicate your app delegate).
I am not 100% sure, but it seems to me that you can't use a SplitView just somewhere in your view hierarchy.
The Apple intended way is to use the SplitViewController as the top level controller. The left side of it can include a drill down mechanism with a navigation controller so you are ably to drill down hierarchies and the right side will present details for the item you select on the left side.
If you need a view with some kind of split mechanism in it, you probably have to code it yourself. Or even better: find some other mechanism you can use in your UI.
How are you switching your view hierarchies now? Maybe you could integrate your existing UI into a SplitViewController?

SplitViewController_Problem

Can we implement split view controller in a normal view controller as in my application we are implementing a tab bar controller and we need to put this split view in some other view and not in window because i just need to know if we are not making a split view based application can we put split view controller in any separate view but not in appdelegate(Window) If Yes please give me possibe solution...
Yes, this would work if you added it to a tab bar controller. Just create all the files for the split view just like XCode sets up a split view project and add the split view controller as a TabBar item just as you would any other view controller. I wouldn't recommend adding the the split view controller in any subview of a view controller in your tab bar as it will not to receive many useful lifecycle events.

Resources