How to implement reusable xib in other uiviewcontrollers? - ios

I have a an app where I need a form (uitextfield + UIButton) that can be reused in several viewcontrollers.
What is the best way of doing this? I am thinking create a myForm~iPad.xib and myForm~iPhone.xib with one myFormVC.h and one myFormVC.m. But then how would I import this form into my Storyboard views or in to ViewControllers through code?
Or is there a better approach altogether?

There are many different approaches to take but it all depends on your business need so you would need to do some research. This is how you would load a xib via code though:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyNib" owner:self options:nil];
MyNibViewController *myvc = [topLevelObjects objectAtIndex:0];

Related

Loading nib files really fast

Trying to load a few hundred UINib consecutively and really fast (one after the other in less than 0.5sec).
UINib contain UIButton and a relatively small sized image (different for each nib).
I have tried this:
UINib *nib = [UINib nibWithNibName:viewName_ bundle:[NSBundle mainBundle]];
[nib instantiateWithOwner:self options:nil];
mainView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
and this:
mainView = [[[NSBundle mainBundle] loadNibNamed:viewName_ owner:self options:nil] objectAtIndex:0];
with the last being a few milliseconds faster and CPU usage is 92% vs 97%.
Which is the best way to load these nibs even faster? Or is there a better way to do this?
thanks
Ok the solution was really easy to this. In case someone else comes across.
Wait for Apple to fix it!
9.1 works like a charm. Everything loads super fast as expected.
Thanks for your input.

Objective C : loadNibNamed method: how does it work?

I would know how loadNibNamed of NSBundle class works; In some document I find something like
[[NSBundle mainBundle] loadNibNamed:#"mynib" owner:self options:NULL];
without return value; just called inside a method (e.g. cellForRowAtIndexPath if I want to customize my cell). In other documents I find:
NSArray* vett=[[NSBundle mainBundle] loadNibNamed:#"mynib" owner:self options:NULL];
In this case, for example, in cellForRowAtIndexPath, I could
return [vett lastObject];
or something like this.
The latter method seems to me clear; I load the nib in a vector and then I use the vector elements. The problem is understanding what exactly do the first:
[[NSBundle mainBundle] loadNibNamed:#"mynib" owner:self options:NULL];
no return value, no cell reference...where are the objects of my nib? how are they handled? I don't understand how it works
For example, you have a subclass UIView with custom nib #"CustomView"
You can load it:
NSArray * arr =[[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil];
CustomView * customView = [arr firstObject];
This method returns an array of the objects in the nib. If you want to instantiate a custom view for example you will want to use the return value in the way anthu describes.
NSArray * arr =[[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:nil options:nil];
CustomView * customView = [arr firstObject];
If however you want to use the xib to configure the file's owner (note that you can pass in an owner to this method), you may not be interested in the array that is returned. E.g. If the xib connects the file's owner's IBActions and IBOutlets to elements in the xib.
[[NSBundle mainBundle] loadNibNamed:#"mynib" owner:self options:nil];
You may also combine both approaches.

loadnibnamed method removes registered cells

I use storyboarding for my iOS application. I also need to use
[[NSBundle mainBundle] loadNibNamed:#"myxib" owner:self options:nil];
method to load a nib file.
After I loaded the nib file,
UICollectionViewCell* newCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"recentItemsCell" forIndexPath:indexPath];
starts giving runtime error.
I think calling loadNibNamed method removes the registered cells (that are registered by storyboard) because when I delete the loadNibNamed line, I can dequeueCell without any problem.
is there any way to load a nib file for a view controller by not removing the prototype cell registrations made by the storyboard?
Thanks in advance
Try to load nib manuallay
CustomCell *cell = (CustomCell*)[[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] objectAtIndex:0];
If the cell is in storyboard , then why do you need to load it using loadNibNamed? If you want to make a separate class for the cell , you can subclass UICollectionViewCell and the call registerClass method in View Controller's viewDidLoad method. In this way you can load the cell which is in storyboard and declare all the properties related to cell in subclass you created.
Hope i am clear.

Custom table cell, custom XIB and long press recognizer - can't make them work together

I have a custom cell that has a button on it. I would like to experiment with using a long press instead.
The code that creates the cell looks like this:
CustomCell *cell = (CustomCell *)[aTableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] lastObject];
}
(etc)
So I did this:
CustomCell *cell = (CustomCell *)[aTableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (!cell)
{
// cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] lastObject];
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCellDwellToSpeak" owner:self options:nil] lastObject];
}
Then I made a copy of the original xib. Deleted the button. Dropped an UILongPressGestureRecognizer on the cell, and created a target by control-dragging form the recognizer to the file's owner:
- (IBAction)longPress:(UILongPressGestureRecognizer *)sender;
However, once I start generating cells, I get this error:
TypOHD[41541:c07] -[UILongPressGestureRecognizer label]: unrecognized selector sent to instance 0x17d0be60
What's up with that?
I never got or found an answer to the question as such. What I did learn was that this plan is potentially flawed. Apparently mixing your won gesture recognizers in with the table view is a recipe for disaster, or at least heartbreak.
I my case, I decided to go with a fixed layout that consisted of just the number of items that would be shown anyway. In my app I was really only using a table for historical reasons anyway, and never needed to display more than 7 items.
A workaround and a hack, but it worked for me.

Best way of loading custom cells from a XIB

I have an app, and I'd need custom UITableViewCells, I've been trying different ways, but no one works for what I want, or simply don't work. Can someone guide me how can I do it? Actually the cell, needs to have some UIImageViews and an UILabel and a UITextView. Thanks in advance!
Create a NIB file with a single UITableViewCell in it.
Loading the cell from the nib is a two liner:
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"TheFile"
owner:nil
options:nil];
UITableViewCell* cell = [objects objectAtIndex:0];
This method will allways access disk when used, so it can be slow. On iOS 4 and later the is a better help class that caches the NIB in memory, for faster creation. Use like this:
// Cache this one!
UINib* nib = [UINib nibWithNibName:#"TheFile" bundle:nil];
// Then do:
NSArray* objects = [nib instantiateWithOwner:nil
options:nil];
UITableViewCell* cell = [objects objectAtIndex:0];

Resources