How can I call an NSView that is already initialized with another window?
newContentView = [[CutoutView alloc]initWithFrame:window.frame]; //make a new CutoutView
[window setContentView:newContentView]; //set it as the contentview of our window
[newContentView release];
newContentView is an NSView subclass and it is set as the contentView of my window. In the NSView subclass “CutoutView” I have it drawing a simple rect.
In another NSView subclass I want to be able to tell newContentView or just CutoutView that it needs to be redrawn by [setNeedsDisplay:YES] but the only way i can think of doing this is making another [[CutoutView alloc] init]; and when I do that and call set needs display nothing works. It says that it is doing it however it is not displaying probably due to the fact CutOutView is already initialized. How can I access newContentView or just CutoutView from where it was already initialized so that way it will actually display. Thanks!
So first and foremost, you need to get a reference to the particular instance of the
CutoutViewyou created so you can tell that particular instance to redisplay itself.You do this in a couple of ways:
window, just call[[window contentView] setNeedsDisplay:YES].So the first problem is that you think your
CutoutViewrepresents an object, that, as you put it, “has already been initialized.” This is the wrong way to think about it.CutoutViewis a class, not an instance.You’ve probably already heard the metaphor about a class and a blueprint, so I’ll use something slightly different.
CutoutViewis a Toyota Prius – not the car, but the model, with the design blueprints and manufacturing process and everything. There’s thousands of Priuses out on the road, the same way there can be thousands of instances ofCutoutView.Your question seemed to imply that you thought
CutoutViewis a car, a physical manifestation of a Prius sitting on a driveway; but it fundamentally isn’t.A class is a model, the design/brand of a Toyota Prius, or even the Nimitz class aircraft carrier, which describes a type of ship (of which there are 10). The classes, by themselves, don’t mean much.
An object, or an instance of a class, is the actual thing your program works with. This is what newContentView in your code is. You just told it, metaphorically: (I hope this makes sense)
^ The USS George H.W. Bush finished ‘
alloc‘ing in 2009 and was commissioned,inited, and delivered to the Navy early that year.^the USS Nimitz is another example of an instance of a Nimitz Class Carrier.
You’ve created an instance of the
CutoutViewclass so you can actually work with it. You can create multiple instances/objects of the same class; there are 10 Nimitz class carriers, after all.Hence,
You can’t tell the USS George H.W. Bush to launch its planes by calling
Just think about it. All you’re doing is spending millions building another carrier you know as “aCarrier” (a much less seaworthy name) and telling it to launch its planes.
Instead, what you want to do is to actually get a reference to the USS George H.W. Bush itself to tell it to launch its planes. So now lets go back to your
CutoutView. You made an instance ofCutoutViewand sent it to your window for display. Then you basically sever the direct radio link between you and that instance by letting go of the reference (because I’m assuming you’re having nothing to do with NewContentView ever again.Fortunately, the window still has a direct radio communication link with the instance of
CutoutViewthat you created. Hence, my first suggested option is to call[[window contentView] setNeedsDisplay:YES], which tells your particular instance ofNSWindowto get itscontentViewwhich happens to be the instance ofCutoutViewyou want to display.The number ways you can do what you want to do is, after all, endless. You can keep a direct radio communication link to your aircraft carrier and give that means of communication to the other instance of NSView you want to be able to send the aircraft carrier messages. You can cut off all direct communication with the
contentViewand let the window handle it all, telling the other instance of NSView to ask the window for a radio line.[Btw, how the heck do you communicate with aircraft carriers anyways? I don’t think it’s radio… In Objective-C it’s having a pointer to the carrier/object’s memory address, but boats are another world entirely.]
Or, if as
rdelmarasked in the comment way up there, theinstanceof NSView you want to be able to communicate with the contentView is actually a subview of the contentView… well, think of it this way. The 15th lifeboat wants to be able to tell the USS George HW Bush to launch its planes. How?[[self parentBoat] launchPlanes]. This translates into[[self superview (a.k.a. parentNSView)] setDisplayNeeded:YES].Also see @Dmorneault’s answer about other ways you can establish radio communication with your esteemed aircraft carrier which you just sent into the Bermuda triangle.
The thing is, building a new
contentView/boatdoesn’t do the trick, and that’s what you’re trying to do and that’s the problem you’re experiencing.I don’t think I explained very well, and apologies if this isn’t new to you, but I thought your question indicated a misunderstanding in how classes work. I also know nothing about aircraft carriers, but that was the first thing I came across on Wikipedia.
Useful conceptual resources: