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.
@"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.
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];
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.
精彩评论