Search code examples
objective-ccocoansviewcontrollernsstackview

Multiple instances of a single content view in NSStackView


I am creating an application that need to have a progress window in which i want to dynamically insert a subview for each item being processed, like Finder's copy files panel window. And also remove it dynamically when processing is done.

I want to use the same NSViewController view for all sub views, and I'm using an NSStackView to manage the views.

But, to make the sub views stay in memory I have to keep a strong reference to them, and the only way I know of is to create a property for each sub view i need to display. Like this:

@propery (strong) NSViewController *myViewController1;
@propery (strong) NSViewController *myViewController2;
@propery (strong) NSViewController *myViewController3;
@propery (strong) NSViewController *myViewController4;
....

I would like to know if there is a better, more dynamic way of doing this? Or do I have to create x number of properties for sub views to know i have enough instances to allocate, because I can't tell how many process views the user will need when running the application and exporting items.

I would therefore like to dynamically allocate each subview AND create a strong reference to it. Is that possible? Or is there another way of doing what I want?

Please let me know if I'm being unclear, I'll gladly explain more to get help with this problem.


Solution

  • You can store the references to the view controllers in an NSArray. Arrays keep strong references to the objects which they contain.