Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa : How to use NSNib several times independently

I've asked how I can make a custom view repeat itself into several separate copies and have been told that I should use NSNib or NSViewController. I have a custom view in an nib file, whenever a user clicks a button, I want a new copy of the nib view to appear, while still maintaining the previous one somewhere else on the screen for up to ten separate views running simultaneously. Since each of these takes some user input before appearing, I assume they must each be separate objects or something to make them be distinct and to not interfere with one another.

I can make the first one of the views appear using

NSNib *nib = [[NSNib alloc] initWithNibNamed:@"IndividualTimers" bundle:nil];

[nib instantiateNibWithOwner:self topLevelObjects:nil];

But that just makes the same view restart every time the button is pressed, I can tell that both views have been combined into that view because a timer that appears on it starts ticking twice as fast, but they should be independently seen in two different instances of the view.

Someone told me I should use set a different file's owner.

So far people have been helpful but not very specific. I don't know what File's Owner I should set, how to programmatically create a new object to hold each instance of the loaded nib (if that's even how it's done) or if I need an individual object for each load.

Basically, I want to know how to take one nib file, and use it as a template to be loaded up to ten separate times, while each of the (up to) ten views is running simultaneously, but independently.

I'd really appreciate any specific help you could give since this is the single biggest problem I've encountered while programming in Xcode. I've been stuck for weeks. Thanks for all the help.

like image 767
Elbimio Avatar asked Aug 03 '11 00:08

Elbimio


1 Answers

When you instantiate a nib file, it assigns the unarchived objects to properties of its File's Owner.

If you connect an IBOutlet UIView property to the root view in your nib, then when the nib is instantiated the newly created object will be assigned to that property.

In order to create a new object each time the nib is instantiated, you need to copy the value of the IBOutlet property elsewhere since it will be overwritten the next time the nib is instantiated.


For instance, assuming that you have connected a timerViewFromNib property to an NSView in your nib file:

@property (nonatomic, assign) IBOutlet NSView *timerViewFromNib;
@property (nonatomic, assign) NSView *timerView1;
@property (nonatomic, assign) NSView *timerView2;

You can add obtain and display two distinct instances of that view as follows:

NSNib *nib = [[NSNib alloc] initWithNibNamed:@"IndividualTimers" bundle:nil];

[nib instantiateNibWithOwner:self topLevelObjects:nil];
self.timerView1 = self.timerViewFromNib;
self.timerView1.frame = CGRectMake(...);
[self.view addSubview:self.timerView1];

[nib instantiateNibWithOwner:self topLevelObjects:nil];
self.timerView2 = self.timerViewFromNib;
self.timerView2.frame = CGRectMake(...);
[self.view addSubview:self.timerView2];

self.timerViewFromNib = nil;
like image 192
titaniumdecoy Avatar answered Sep 20 '22 15:09

titaniumdecoy