Adding a split view controller to a single view project in Xcode 5 - ios

I have an application that is working quite well, but the customer wants to combine two of the views (UIViewControllers) in to a single view (a Split View Controller).
The master part of the view will be a table with a list of Staff in a TableView, and the detail view will be made up of two Table Views - one that contains details about the member of staff (name, phone, ext, office etc) and the other with training records (courses, qualifications and so forth).
When you click on the master table, it selects the detail records for the given member of staff and displays them in the detail view.
I have no storyboard in the application, and use [self presentViewContoller] to switch from view to view (because there is a lot of conditional processing on what view is required and a storyboard seemed to be too restrictive)
However I have tried the following :-
Creating a SplitViewController (right click, New File, UISplitViewController), but it seems to be empty with no master or detail views. Also, when I try to make it appear, it either doesn't appear, or causes it to crash (tried to present a SplitViewController modally).
Creating a "fake" split view controller, by putting two views on a normal view controller, then putting the required tables inside them. However I can not find a way to present any borders or the like, and the whole thing looks a tad unprofessional and stupid.
So my question is basically this :-
Is it possible to create SplitViewControllers in single view projects, and use them?
Or, if not, is it possible to put borders around views, or tables, or draw a single straight line on a view?
(If at all possible, I would prefer not to have to rewrite my entire application and just add this SplitView controller or the "fake split view Controller")

You can use container views:
Create a new root view controller and add two container views to it. This will create two embed segues that you can point to the view controller's whose views you want to appear in these containers.

You cannot control two UIViewController simultaneously.
So, try displaying two UIView or subclasses of UIView like UITableView.
Displaying Two UITableView is a little tiresome.
Take a look at this question Two UITableView in the same view
As for borders around views, you can put UIImageView or something as border lines.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(159, 0, 2, 640)];

Related

Creating individual View Controllers for each view iOS?

This is a very simple simple question, and I believe the answer is “yes, it’s a best practice,” but I just wanted to ask the question.
When creating new views in your storyboard in Xcode, is it a good idea to create individual custom view controller files to handle each view specifically?
Meaning, if I create a new view called “login quiz,” should I create a “loginQuizViewController” that will handle all the code I write for that view?
Short answer:
Yes, each storyboard scene generally has a unique view controller class associated with it. A view controller has a root view, which in turn may have many subviews below that, and each subview might have further subviews. This collection of views is collectively known as the “view hierarchy”.
Long answer:
Yes, each storyboard “scene” has a view controller associated with it. And generally this view controller class is a particular UIViewController subclass which is unique to that particular Interface Builder scene. But you don’t necessarily have to have a view controller subclass and might, for example, use one of the existing classes (e.g. this is not uncommon for navigation controller scenes or tab bar controller scenes).
But if you have any custom logic associated with a particular storyboard scene, then, yes, you would generally have a unique view controller subclass for that particular scene.
Two minor clarifications:
You refer to the “quiz view”.
That’s fine for colloquial purposes, but for the sake of clarity, when we’re talking about everything for this quiz, it’s really a complex hierarchy of views, not just a one.
A single “quiz” scene will be associated with a unique view controller class, and the instance of that view controller class will have a single “root view” (identified with the view property), but that view will have a whole bunch of subviews (e.g. image views, buttons, labels, etc.), and some of those may have subviews of their own.
So one storyboard scene has its own unique view controller class, but is associated with a whole hierarchy of views.
We often think of a scene, and its associated view controller, as representing everything you might see at any given point in time, but it’s not always a one-to-one relationship. Sometimes what you see is composed of several storyboard scenes and their respective view controllers.
For example, if you are using a navigation controller, the navigation controller takes care of the navigation bar at the top, and your view controller might take care of everything under the navigation bar. What is visible on screen is composed of these two view controllers.
Or, in a more extreme example, we can define our own view controller containers. In the example below, when we present A, we can define the bottom half of the screen to be managed by a completely separate scene, B, which has its own view controller:
In this case, both A and B have their own IB scenes and their own respective view controllers. Achieve this by adding a “container view” to the A scene (the light blue UIView in the above screen snapshot).
But the main point is that what you see on screen may be captured by a single storyboard scene and view controller, or it might be composed of several.
A view controller can and probably will contain multiple views apart from the root view it has. That means that usually a view controller owns one ore more views that are subview's of it's own root view. Those views are often also controlled by the same view controller.
When coming from the storyboard most items you see are actually representations of view controllers (others are placeholders). "Login Quiz" sounds like a screen in conceptual terms, so (without knowing your details) it would probably make sense to create a LoginQuizViewController.

Adding a UICollectionViewController inside another UICollectionViewController

I am writing a project where I need to display a list of players (avatars) in a horizontal collection view controller / list, let's call this a HUD.
I want this HUD to be common, that is to say: I know this one component will repeat many times throughout the app.
I do not want to write the Collection View code over and over again.
I want to add the collection view to an existing view controller which already has a collection view.
Almost like an in-line Master-detail display where the master does not change from page to page.
Is there a way in Swift where I can instantiate the HUD collection View and append it to whatever View Controller I am in.
I have included a picture.
Thus my query is this:
How do I include a collection view controller inside another View controller that already has a UICollectionViewController?
How do I share this HUD component across multiple screens in the app?
Many thanks
I used a container view to solve my issue. This issue is now closed.

How to create simple tableview and load data using Swift in iOS

I am trying to create an app for iOS using Swift but running into problems with the very basics.
To keep it simple I just want the app to initially be a single view application with a button and some sort of list view on the page. I believe a TableView is what is recommended here. When I click the button, I just want it to populate the list/table view with some entries, that's it. To start with, I don't care if these entries are hard-coded, I just want to get something working.
I have been looking at different samples but I am getting confused. Some of them seem to suggest using a TableViewController others don't. When I use a tableview controller, the UI I had created seems to get completely replaced with just an empty tableview list and the button is gone.
I previously have developed apps in Windows phone and found it a lot easier. I'd just add a listview object and in the click method of button, add the items programmatically etc. But this is my first time trying to create an iOS app and it seems a lot more confusing. There are delegates, controllers, views all seemingly needed in order to do something very simple.
Can anyone give me some basic step by step instructions about how to add a tableview to an application and load some data into it through a button click?
Make sure you are clear about the difference between a view and a view controller.
iOS uses the MVC design pattern (Model View Controller).
A view object displays contents to the user and responds to user interaction.
A model object stores state data.
A controller object drives the app logic and mediates between the model and the view.
A UITableViewController is a special subclass of a UIViewController who's job is to manage a table view. It has some extra support in it that makes it a good choice for managing a table view, BUT... there is one annoying thing about it. It is designed so the ONLY view it can manage is a table view. You can't use a UITableViewController if you want to add buttons, labels, and other UI elements to your screen outside of the table view.
What I usually do is to create a create a table view controller, create a separate regular view controller, add a container view to the regular view controller, and then use an embed segue to embed the table view controller inside the view controller. (you just control-drag from the container view to the table view controller.) That way you get the best of both worlds. You may want to create a protocol that the table view controller would use to communicate with it's parent view controller.
You should be able to find a tutorial online on setting up a table view controller as a child of another view controller using container views and embed segues. It's quite easy.

iOS nested viewcontrollers and interface builder

I'm very new to iOS development and i'm trying to get my head around viewControllers and how they're used with storyboards. I've read a lot of the official documentation on viewControllers, Objective-C and i've played around with xCode to familiarise myself with it.
I'm trying to create the following:
Main Window
- My starting view should contain three "frames" (i'm trying not to use iOS terminology as I don't want to confuse things)
Frame 1) A rectangular area stretching across the screen containing three buttons. Each button when pressed causes frame 2 below to change between different views. This frame kind of acts like a tab view controller but with the tabs at the top and the view being changed in frame 2.
Frame 2) Contains two columns of scrollable table items. When an item in the first column is selected, it causes the contents in the second column to change.
Frame 3) A simple scrolling text label.
My thinking was that I could achieve the above as follows:
InitialViewController
Frame 1) One nested/embedded containerViewController containing 3 buttons
Frame 2) Two embedded tableViewControllers. I was thinking that TVC 1 could pass the cell selected action up to the mainViewController (via delegation) which would then instruct TVC 2 to change it's contents.
Frame 3) A scrollable view
I'm trying to create the above in a storyboard and i'm stuck with frame 2, I've created Frame 1 (a containerViewController containing 3 buttons) but when I try to drag a tableViewController onto my main view it's not letting me. Dragging it onto the canvas is fine, it just wont let me drag it onto the main view.
Have I misunderstood how to use viewControllers (and therefore my planned design is wrong) or is it something i'm not doing correctly in the xcode interface designer?
Any help really appreciated!
If you don't use the correct terminology, then you probably won't be thinking about your structure correctly. Frame 1, for instance, should be a UIView containing three buttons, not an embedded controller. "Frame 2", could be 2 embedded table view controllers, but I think a 2 component UIPickerView might be more appropriate. As for embedding controllers in another controller's view in the storyboard, you do that by dragging in a container view, and that will give you a new controller automatically.
I think you have misunderstood view controllers. A view controller can have multiple subviews (buttons, a picker view, a label) that it can host, you don't necessarily need to have multiple view controllers to do what you're trying.

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

Resources