Using 2 views in 1 xib file - ios5

I want to create a view that will appear only once (on first time). After, I want to show another view.
Can I declare 2 views in 1 xib file?

Yes but you you will need some funky looping code to extract the proper view or you could get the view via index, if you know it will always be at a given index. Personally this is not an ideal solution for me because I like to have strong pointers to all objects.
Your Could create a ViewController subclass with 2 views and then bind each view to variable via interface builder. This might be a little cleaner?

You can, but there's no reason to do so. Just use two separate view controllers, each with their own view (in separate nib files).

Related

Different Representations of Data in 1 View Controller

I am using Xcode 6 and Swift.
I have some data that I'd like to represent in two or three different ways using a segmented control.
The first view is a list, the second is a grid, and the third is a table with some labels.
Despite the different appearances, they are all drawing from the same data source.
What is a sensible way to achieve this?
Should I drag 3 views onto 1 controller (the same view controller), and do all of my design/storyboarding within each view, add constraints as needed, and then just show/hide each view accordingly?
Is there a different or better way to do this, or is this about right?
Thanks so much!
Looking at the requirement, i feel TabViewController is best. Using segmented controller you need to take care of switching views yourself. You can have a TabViewController with 3 viewControllers and these view controllers can draw waterer data they need from your data source.
There are two elegant ways to this effectively and clearly.
1)You can use different xib for each 3 views and on runtime load appropriate xib of the different views.This approch should used if you have small and clear views.There is not much work need to handle like dataSource and delgations.The three views will manage by only one view controller.It will make all your views separate and easily managable.You can load diffrent views as
//Your view property
var uiview :UIView?
//load nib from bundle in view
self.uiview = NSBundle.mainBundle().loadNibNamed("myXib", owner: self, options: nil)[0] as? UIView
2)Use container viewcontroller.By doing this you can add the different controllers to your parent controller.This approach is very clean as different views are handled by differnt controllers but it is hard to write.Use this if you have heavy work need to do by each view.See WWDC video to get more help https://developer.apple.com/videos/wwdc/2011/?id=102
I think in your case 1st approach is appropriate use the three different xib and load it on runtime and add as subview of your viewController

1 ViewController per view?

I wanted to know if I have to create a new ViewController for each view I have. Let's say I have 3 views do I need 3 viewControllers or does 1 ViewController can manage all 3 views?
Edit: Views are Objects that I create with .xib file so following the description given me by Xcode 4.3.3: represents a rectangular region in which it draws and receive events.
My situation : What I want is the main View showing 3 buttons, each button leads to a new View. That's about it each of the 3 views will have about the same thing, a tableView to display data parsed from an XML.
Thanks again!
EndEdit
Thanks
Both are possible, depends on your needs. View controllers can easily manage more than one (sub)view (e.g. every UI control subclasses UIView and you can have many in a single view controller). Sometimes it's more convenient to put views in different view controllers, sometimes it's not.
Depends on your situation and what you want to present to the user. If those 3 view controllers have very similar functions and only minor differences, then you could use the same class and have an instance variable to indicate which mode you're in. For instance if you have a list of Songs, Playlists or Videos, those could be the same class, with an enum variable to differentiate between the 3 modes. If your views are significantly different however (like a Song list and a Video player) then you should usually have separate classes for them.
For iOS, general best practice has been for a single view controller to manage a single "screen (or "window", but of course there is only one of these on an iOS device). This view controller may support a variable number of view objects (certainly not a ratio of 1:1 views/view controller).
As of iOS 5, nesting multiple view controllers managing multiple views in single window is now technically supported, but I still consider it a practice best avoided.
One window = one view controller = multiple views.
In one view controller you can manage any number of views.
It is preferable to use a viewcontroller per view. the advantages of the approch is as follows:
Any time you can convert the Template of the application. (eg. from table view to tabview or split view can make quick change)
with each view controller you follow the MVC architecture.
if you have n controller than try to manage n MVC model so that you can shift any model any where easily.
Memory management will be easy (Push and Pop manage memory automatically.)
if we have sigle view controller and n number of view than it will be quite difficult to manage memory.
Also we need to keep the screen ID for each view and need to mantain also their switching.
there are N number of advantages if you use N numbers of viewcontroller over N number of views.

Storyboard done, Do I need to create .h and .m View Controller file for each View created?

I have created Storyboard with several views calling each other, now I need to create the code
I notice that XCode didn't created .h and .m controller files for each View from storyboard.
Should I create them manually?
Should I keep only one controller? (or few depending of separation of concerns on MVC)
Is there a pattern for developing this?
thanks
The usual approach is one view controller pr. screen full of content. You can imagine having one view controller for a tableview, with any sort of content, and then another view controller that presents that content in a new screen full of content if a row is pressed.
Normally when you have subviews inside of your view controllers, you wire them up in interfacebuilder. Then for instance if you want to populate a view that has a uiimageview and a uiactivityindicatorview inside it, you can control their behavior and how their populated from the view controllers code. You could also if you want something very generic and you feel that one view will probably take up a lot of code in your view controller, create a uiview subclass for it, and then set the class in interface builder.
Did this help? Please let me know if you need more clarification.
It's entirely up to you whether you have a ViewController for each view. If you have many views I would recommend it. Even if you have 2 or 3 views you probably still should. Things can get really confusing when each view has a different task but all have similar IBOutlets.
TLDR; Personally, I would say it was good practice to have a ViewController for each view if each view has a separate task.

iOS: When to use multiple NIB's or multiple views on same NIB?

Can anyone explain when it's better to use multiple views on the same NIB file than using multiple NIB files?
A NIB file can contain more than one view, so there should be times when it's better to use one method than the other.
Have i been too vague?
Best Regards
Fak
Personally I think using one view per NIB is better because you can use the name of the NIB file to load it. It also makes it easier to deal with changes while your App is evolving and helps if multiple developer / creative designers are working on your App.
The only place where I could thing of a good reason to put multiple views into a single NIB is when the views belong together. An example would be multiple cells for a single table which are then selected by the data type of the row or the section they are used in.
The downside of having multiple views in one NIB is that you need to load the views manually when creating the View Controllers so I suggest only using a single View per NIB for view controllers.

How to embed one nib into another?

There are many view that share a common part in my current project so I just visualize a way to encapsulate the common part into a nib file that could be shared by many controllers by embedding it to their corresponding nib file. Although I know how to do it programmatically, I still believe there should be a way to achieve this simply in Interface Builder. Are there anyone who has achieved this and would you like to point the way out?
You can add in your NIB file a view called "Content", for example. In your code, the only thing you need to do is add your new UIViewController's view inside the "Content"'s view. This way, you can achieve a view that holds common data, while the only thing you do is to change the view inside the "Content"'s view.