Cocoa: How to use NSNib several times independently

I asked how I can make a custom view repeat in several separate instances, and I was told that I should use NSNib or NSViewController. I have a custom view in the nib file, whenever the user clicks the button, I want a new copy of the nib view to appear, while preserving the previous one in another place on the screen to view up to ten separate views at a time. Since each of them accepts some user input before appearing, I assume that they should be separate objects or something to make them clear and not interfere with each other.

I can do the first of the views using

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

But it just repeats the same view each time the button is pressed, I can say that both views were combined into this view, because the timer that appears on it starts ticking twice as fast, but they should be independently visible in two different view instance.

Someone told me what I should use to set a different file owner.

So far, people have been helpful, but not very specific. I don’t know what the file manager should be, how to programmatically create a new object to store each instance of the loaded nib (even if that’s how it is done), or if I need a separate object for each download.

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

I would really appreciate any specific help you could give, as this is the biggest problem I encountered when programming in Xcode. I was stuck for several weeks. Thanks for the help.

+6
source share
1 answer

When you instantiate an nib file, it assigns unarchived objects the properties of its file owner.

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

To create a new object each time you create an nib instance, you need to copy the value of the IBOutlet property to another location, since it will be overwritten the next time you run nib.


For example, if you connected the timerViewFromNib property to the NSView in the nib file:

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

You can add the receipt and display of two different instances of this 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; 
+6
source

Source: https://habr.com/ru/post/894155/


All Articles