How to use UIViewController in non-modal way? - uiview

I'm having a uiscrollview with 3 slots, each holding a tableview with different data.
As you can imagine, handling 3 tableviews on the same viewcontroller is a bit of a mess.
So I was thinking about using multiple uiviewcontrollers to handle the different logic required for each tableview. However I can't seem to do this since uiviewcontrollers load in a modal way/blocking the entire previous view: this leaves me unable to continue to use the uiscrollview placed on my root view controller.
Is there a way to solve this? Or pack UIView logic and behavior in some sort of stub/hidden uiviewcontroller that doesn't block the view below?

You are looking at this the wrong way. Separating your tableviews is a good idea, but there is no need to use a view controller.
Let's say you have three tableViews: tableViewOne, tableViewTwo, and tabelViewThree.
The problem (as I understand it) is that all of your logic code (datasource and delegate methods) are getting all mixed up. So, just create a subclass of NSObject for each tableView and use that object for the datasource/delegate. Just make sure you set the datasource/delegate properties of the tableView to the right object.

Related

Is there a right way to implement TableView

I'm learning swift and I have gone through tutorials where some developers delete the View Controller entirely and just drag in a TableViewController and start using that. While other developers in tutorials create a TableView from within the View Controller.
Is it better to delete the View Controller and add a Table View Controller or just work with a TableView within the View Controller?
It is all about the requirement. Both are correct way. Don't be confuse.
When you using TableViewController then it only behaves with the UITableview. i mean controller totally dedicated itself as tableview.
When you using TableView inside UIViewController then you can create whole UI deign in your own way.
Note: As you'r new so I suggest you to go with TableView Inside UIViewController because you can achieve any type of UI with this even you can create ditto functionality(UITableViewController) with this but vice versa not true.
For eg.
tableView inside UIviewController
You can do it in either way. If your viewcontroller is going to have a tableview alone, then u can use a UITableViewController or else use UIViewController and set the delegates and datasource

Multiple instances of the same UIView in UIScrollView

I've used this answer to implement a UIScrollView with paging.
It works great as a start, but what I want to be able to do is for each page in the UIScrollView, I want a separate instance of the same UIView.
For example, say I have ContentViewController, and I want to use 4 instances of it's UIView;
1 for each of the pages on the UIScrollView - how do I go about doing that?
My initial thought was to instantiateViewController(withIdentifier), and then reuse it's .view' property inside thefor` loop, but this only gave me the correct view once, and the other 3 were blank subviews.
Any help would be much appreciated.
Thanks!
You could instantiate the ViewController multiple times (in a for loop) and then fetch each controller's view and add it to the scroll view. But you have to be aware that now you also have 4 Controllers, each for one view.

Multiple UITableViews visible at once in one UIViewController

I have seen questions asked about mutliple UITableViews in one view but they all have only one table visible at a time. I am after something different please.
In my head I want four UITableViews visible in one UIScrollView inside one UIView. The four tables will be visible and populated at once. They will have a dynamic number of rows each so the scroll view will allow users to scroll off of the page to see rows that do not fit.
The tables would be two side by side and then below them the next two side by side so that you end up with a 2x2 square.
I can (sort of) wrap my head around how to code this in the controllers etc. but I cannot figure out how to organise the hierarchi. I have tried using the storeboard to layout the tables inside the view but 9 out of 10 attempts to drop controls in fail as I am obviously not fully understanding this.
Do I need to generate the UITableViews in the UIViews implementation file and add them as objects to the UIView? Or can I use the Storyboard?
Could someone please explain how the hierarchi of objects would be structured?
In my head it would be:
UIViewController
-> UiView
---> UIScrollView
------> UITableView
------> UITableView
------> UITableView
------> UITableView
But trying this in Storyboard doesn't work. I assume each UITableView will want its own UITableViewController and what I have read in other posts I would likey need to do this connecting in the UIViewController implementation file.
Any help is appreciated.
I think you might try to drag UITableViewController into your view Controller, at least I don't have that problem to add 4 table view into a scroll view.
here is how i added it
1.> Drag the scroll view control into view controller
Your view controller should look like this:
2.> Drag the table view control into the scroll view, and set the size and position of that table view
Your view controller should look like this:
3.> Then drag all the rest 3 table views onto Scroll view
But i would like to suggest a couple of things in your case
no using that much table view in the same view controller, it's a chaos in your codes to maintain all them. There are always better
options than 4 table view, maybe consider collection view. or even
separate the use flow.
If i were you, i won't use table view inside Scroll view, they are like scroll view inside scroll view, if you don't design the
interaction very very well, they become extremely hard to use.
If you still want to use four table view in the same view controller after all, you want to pay extra attentions on your table view datasource and delegate. very carefully handle all the cases.
Hope that helps you.
Tableviews are very customized scrollviews. I wouldn't put 4 of them on a scrollview, this wouldn't be very intuitive for the user as your finger would scroll the view in many ways depending on where exactly it touches the screen.
Instead, try having your 4 tableviews in a 2x2 pattern directly onto a simple UIView. This can be done inside the Storyboard.
As for filling up and using them, you have 2 ways :
A) Your UIViewController is the delegate and datasource of each of the 4 tableviews. When executing, you perform a switch on the first parameter (the tableview that called you) to determine the appropriate course of action.
B) You create 4 classes that each handle a single tableview, instanciate 4 objects inside your UIViewController and assign the UITableviews' delegate and datasource properties to these objects.
All technicality aside, 4 tableviews in a single screen is pretty crowded. Your design will definitely not fly on a iPhone, so I'm assuming iPad only. Even then, you should make sure that everything is visually appealing and the purpose of each control is clear. It's up to you, but I'd hate to see you work hard on an application only to see your efforts wasted because your visual design doesn't appeal to your users.
If the table views take up the entire region of the scroll view then they wont let any scroll events past to the scroll view that contains them, unless the scroll is horizontal.
For a simple one to one between a table view and a view controller, I would make each table view part of it's own UITableViewController (so you have four), and then make a UIViewController that adds each of the UITableViewControllers to it as a child.
This way you don't have to do any fancy logic around if statements on which tableview is asking for data, because the table view controllers only have one table view.
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

When to use UIViewController Containment? (delegation overload)

I have a view that contains a mapview and a tableview, similar to how to the facebook nearby places screen looks in the facebook app looks.
I put the screen together using interface builder. I have outlets to the tableview and map. And, I set the UIViewController that controls the main view to be the delegate for both the mapview and table view. Everything works perfectly.
However, this main UIViewController is now pretty messy because it has to respond to methods on behalf of it's own view, the mkmapview and the uitableview. However, data is somewhat shared between the map and the table view.
I am wondering if this is where UIViewController containment comes in? Or, if I should should just make custom objects to act as delegates for those two views. I am looking for the correct design pattern to use in modern iOS. A few years ago just mashing it all into the same controller would be the way to go. Maybe it still is the way to go.
Both your ideas are good thoughts, but...
Controller containment is typically used for more navigation related purposes, like a UINavigationController.
Custom objects that do some of the delegate/datasource work, that's a good thought too, but a little clumsy to the extent that they share data with the view controller and each other. This might be a good solution especially if other view controllers will need the same delegate/datasource behavior.
A third alternative, and probably the best of the three, is to create class extensions of the view controller (see apple doc here). These can be thought of as parts of your class in different files.

To subclass or not to subclass : that is the ...?

I am building an app with many views and subviews.
I am trying to figure out how to approach creation of the subclass.
one simple example:
I have a table view with heads view and footer view. (I have much more complex views then this one, but I am trying to explain my point with a simple one).
In the beginning I didn't want to create views subclasses for every view, but I found that my code is getting complex and hard to navigate in.
Then I found my self creating a view for every subview in the main view. The truth is that I like it better cause my code is clearer to me. Th only thing is that I don't know what are to cons and pros of each approach.
How do you decide when to create a custom view or create it's code in the main view?
Thanks
I usually create a UIView subclass only when there is specific functionality that would be best done in such a class. This might be that the view is a control such as a keyboard to enter a value or a UITableViewCell subclass.
Otherwise I would generally not subclass UIView but just create the view hierarchy in Interface Builder or in code and then use the UIViewController subclass to do all the business logic.
Basically, in general with iOS programming, you want to have all the logic of how a "view" (or screen, or whatever you want to call it) in a UIViewController subclass. It's hard though to really give good advice without fully understanding your hierarchy.

Resources