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.
Related
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.
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];
Man, it's annoying how I have to keep referring back to my old xcode projects to find out how to setup a custom cell in my cellForRowAtIndexPath delegate method only because of the magnitude of methods I've seen.
What is the correct code for setting up a custom cell in the cellForRowAtIndexPath method without the use of storyboards?
I am currently doing something like this:
//This works for me btw
static NSString *simpleTableIdentifier = #"customcell";
CustomTableViewCell *customCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (customCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
customCell = [nib objectAtIndex:0];
}
Then I see other people using loadWithNibName and others that are registering their nibs in the viewDidLoad method using the registerWithNib method.
What in the world is the correct way to setup a custom cell in the delegate method?
I have also been told recently that I dont need to call the dequeueReusable.. method when I'm dealing with custom cells. Can someone please clarify the correct way to setup your cellForRowAtIndexPath delegate method once and for all for me. Without the use of story boards. I have correctly setup my custom cell view with its respective .m and .h files.
If it is CustomTableViewCell nib file exist in same UITableViewController nib your code will works fine. Otherwise you need to load nib file in cellForRowAtIndexPath:
if(cell==nil){
NSArray * nibContents = [[NSBundle mainBundle] loadNibNamed:"CustomTableViewCell" owner:self options:NULL];
NSEnumerator * nibEnumerator = [nibContents objectEnumerator];
NSObject * nibItem = nil;
while ((nibItem = [nibEnumerator nextObject]) != nil) {
if ([nibItem isKindOfClass:[UITableViewCell class]]) {
cell=(CustomTableViewCell*)nibItem;
}
}
}
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.
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];