views:

66

answers:

3

Hi all,

In my application,I need to load different .xib in different tableView cells depending upon the category of data which I'm getting from parser. I wanted to ask that is it possible to create different .xibs belonging to same class as it'll reduce the load as I have almost 13 categories so 13 .xib files.

Thanks in advance.

+1  A: 

If you mean that you'd want to have several NIBs for the same view controller then it's most certainly possible. In fact that's how application localization is done. You can then load the specific NIB when you initialize your controller.

NSString *nibName = @"DefaultNibName";
if (someCondition) {
     nibName = @"SomeOtherNib";
}

YourViewController *controller = [[YourViewController alloc] 
                                 initWithNibName:nibName bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
RaYell
No, what I mean is can I create multiple .xibs with same class as owner?
neha
That's exactly what you do. You just create a standard controller with NIB and then copy this NIB bunch of times. Each of those NIBs will have the same view controller as owner and using the code similar to what I've posted you will be able to pick the one you want by their name.
RaYell
Ok RaYell.. Thanks... That helped..
neha
@RaYell: If nibs have different outlets then? how will you handle?
Manjunath
you can define all the outlets in the view controller and connect only some of them to your NIB. To be honest I only used this method to have two NIBs with the different version of the same page. If you want different logic behind each of those NIBs having several view controllers will probably be easiest.
RaYell
Two nibs may considered as ok but think 10 or more nibs with at least 10 to 15 outlets and some 10 actions. Is it feasible? Even if we don't have any outlets and actions, then also we should maintain separate controller isn't it?
Manjunath
As I said, it only makes sense if the NIBs are similar. If they are totally different, there isn't much sense dealing with all of them in one view controller is it?
RaYell
When nibs are similar then why do you need to have them? Instead, only one nib will be sufficient right?
Manjunath
Not always. Imagine your application is supporting 10 languages. The content is similar on each of the NIBs but the layout will be slightly different. Of course you can also create one NIB add outlets for everything and change language contents in your view controller but it's not very convinient and you will never be sure if translated texts will look well in different languages. So in this case it's better to have several NIBs, one for each language.
RaYell
oh my god!!! I hope you know localization! If not, then please read about localization :-)
Manjunath
+1  A: 

But how will you make connections(outlets) which will be different in different .xib files?

will you keep lot of outlets and actions in a single controller? If so, then think accidenlty you try to access the outlet which is suppose to be of some other nib. Then what will happen?

If you try to do so then you view controller will look like a garbage. So please dont try to use only one controller for loading more than one .xib files.

Manjunath
Is it possible to create multiple views in same .xib?
neha
yes you can create any number of views
Manjunath
yes you can create any number of views. They will be in the form of outlets in the single view controller. ex: say if you have 5 views then, your controller must have five outlets to get connected to them. But tell me one thing, you are so bothered about creating 13 view controllers and If you load 13 views in a single nib, then what will happen to your memory?????
Manjunath
What do you think is a better option performance-wise and memory use-wise : creating 13 view controllers or 13 views in single nib and loading them as per need?
neha
When you load the nib, all your objects in nib will get created. Means all your 13 views get created. So better you create 13 view controllers in need basis. before creating next view controller you release the previous one. In this way you will be having only one view controller alive and it will give good performance and as well as memory is also saved :-) think on it.
Manjunath
Thanx Manjunath... It really helped..
neha
+2  A: 

@"I wanted to ask that is it possible to create different .xibs belonging to same class as it'll reduce the load as I have almost 13 categories so 13 .xib files."

The xib files are not a burden on memory unless they are loaded, in which case, the file's owner object is created. So keeping this in mind, it doesnt matter how many nibs you have for your class, for an object of each viewController class, the corresponding xib is loaded. So ultimately you have to put in a check condition as stated by RaYell, it would be better to introduce that check where you spawn the viewController object instead checking the condition for loading appropriate xib.

Dont bother about creating 13 viewControllers, you will find it easier to make changes in your project later if there are changes in requirements. You will appreciate this approach.

If you create only one UIViewController sub-class and load one of 13 xib's based on some condition, say, there comes a requirement that you add a button / label / textField in the 13th xib ONLY and need its reference in your viewController class. How would you achieve it, you maintain an IBOutlet in the common viewController class and introduce the if-else check to see if it is the 13th category. The code becomes untidy with lots of if else conditions.

Raj
Thanks Raj.. It really guided..
neha
Also, is it possible to create multiple views in same .xib?
neha