iOS nested viewcontrollers and interface builder - ios

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.

Related

A permanent navigation bar with UI elements

Our app has some upper view, that is visible all the time.
This bar has a UITextField, UIButtons, side scroller, and segment control, and they are dynamic.
When you hit them, the view behind them(full screen) is changing.
I was thinking about navigation control, or tab bar, but seems that they can't have a text field and a scroller on them.
So my main thought was to create some COSTUM view of my own.
Question is , how can I create a view in storyboard, and add it as a constant view, than create some other views(+viewcontrollers) that will be changed according to that upper bar?
I want to create 5 views in storyboard, and switch between them according to the bar.
Sounds like a job for a containment view controller to me. I've used technique many times to both create a set of static controls on the screen which you describe and inject reusable content into an app in several locations.
The basic concept is:
Setup you Heads Up Display(HUD) with all the UI you want (this will be your base UIViewController).
Create a UIView in it and call it your contentView or something of the like. This is where all your dynamic content will appear.
Then your backing view controller adds another UIViewController as a child and tell it to show it's view in the contentView you specified.
Your view controller continues to remove and add children putting their content into the contentView as needed.
If you are unfamiliar with the technique there are many tutorials(by NSCookbook) of do a web search for "view controller containment tutorial". There is also a good WWDC (2011) video introducing the concept Session 102 - Implementing UIViewController Containment.

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

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

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)];

iOS6 - Changing embedded subview in a view

I'm looking for some ideas what's the best way to implement the following behaviour (and a starting point in code/pseudo-code if possible):
BookViewController with ViewA - contains some UILabels (e.g. information about a book)
MainViewController with ViewB - contains some UI elements and displays ViewA in the middle (one view at a time)
The user should be able to swipe inside ViewB to view different books i.e. I will need to manage a number of ViewA views, create new ones and populate them with new data, and then replace the current instance visible in ViewB. So effectively I'll be changing views in the centre of the screen. What's the easiest and most efficient way to accomplish this functionality? Any suggestions comments are appreciated - thank you.
You just need one view controller. It should control a scroll view with paging enabled at the center and e.g. three book views, one visible and the ones left and right of it. The controller should manage the data displayed in these book views.

Use a UIView ( created using xib) multiple times as a subview to the same view

My requirement is that I want to create a view which has to be added to a scrollview multiple times ( 12 times for each month ) with a pagecontrol , loaded with different data.
The requirement specifically is to use a scrollview/pagecontroll with previous/next page visible.
I have successfully done that by manually creating the view, but when i reduce the alpha of the added view, the alpha of all the ui components added to it is reduced.
hence i need to create the view using xib.
But view instantiated using xib are getting added to the scrollview only once. remaining pages as just blank.
Any ideas on how to proceed here will be highly appreciated.
Go back to creating the view manually, but instead of adding ui controls to it, create another parent view and add your created view and the other ui view as children to this parent view,
Now when u decrease the alpha on the view, other ui controls will not be affected since they will not be children to this view.
So your new view arrangement would be:
Parent View -> your created view (the one that you want to change its alpha)
-> other ui controls
So your view will not be the parent of other ui controls
You will have to create different instances of same UIView class and add as subview on UIScrollView. And for alpha, the alpha set for parent view will override all its child.

Resources