从已经初始化的其他类调用NSView [英] calling NSView from other class thats already initialized

查看:129
本文介绍了从已经初始化的其他类调用NSView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调用已经用另一个窗口初始化的NSView?

  newContentView = [[CutoutView alloc] initWithFrame:窗框]; // make a new CutoutView 
[window setContentView:newContentView]; //设置为我们的窗口的内容视图
[newContentView release];

newContentView是一个NSView子类,它被设置为我的窗口的contentView。在NSView子类CutoutView中,我绘制了一个简单的矩形。



在另一个NSView子类中,我想要能够告诉newContentView或CutoutView重新绘制 [setNeedsDisplay:YES] 但我唯一的方法,我可以想到这是做另一个 [[CutoutView alloc] init]; 和当我这样做和调用集需要显示没有什么工作。它说,它是做它,但它不显示可能是由于事实CutOutView已经初始化。如何访问newContentView或只是CutoutView从它已经初始化的方式,它实际上将显示。谢谢!

解决方案

首先,您需要获得对 CutoutView



您可以通过以下几种方式完成此操作:




  • 如果您仍然有对窗口的引用,只需调用 [[window contentView] setNeedsDisplay:YES]

  • 使newContentView成为特定类的属性,所以其他类只能访问它...从根本上,






所以第一个问题是你认为你的 CutoutView 代表一个对象,就像你说的已经初始化。这是错误的思考方式。 CutoutView 是一个类,而不是一个实例。



你可能已经听说过一个类和蓝图,所以我会使用略有不同的东西。 CutoutView 是丰田普锐斯 - 不是汽车,而是模型,与设计蓝图和制造过程和一切。道路上有成千上万的Prius,同样的方法可以有成千上万的 CutoutView 的实例。



您的问题似乎暗示您认为 CutoutView 是一辆汽车,普锐斯坐在车道但它根本不是。






类是一种型号,丰田Prius的设计/品牌,

  • Apple文档


  • 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 CutoutView you created so you can tell that particular instance to redisplay itself.

    You do this in a couple of ways:

    • If you still have a reference to window, just call [[window contentView] setNeedsDisplay:YES].
    • Make the newContentView a property of a particular class so your other class can just access it... fundamentally you'll have to understand how classes really work in order to answer your question, because this isn't a technical problem, it's a conceptual one.

    So the first problem is that you think your CutoutView represents an object, that, as you put it, "has already been initialized." This is the wrong way to think about it. CutoutView is 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. CutoutView is 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 of CutoutView.

    Your question seemed to imply that you thought CutoutView is 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)

    NimitzClassCarrier *USSGeorgeHWBush = [[NimitzClassCarrier constructBoat] prepareForLaunch];
    

    ^ 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 CutoutView class 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

    NimitzClassCarrier *aCarrier = [[NimitzCarrierClass construct] prepareForLaunch];
    [aCarrier launchPlanes];
    

    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 of CutoutView and 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 CutoutView that you created. Hence, my first suggested option is to call [[window contentView] setNeedsDisplay:YES], which tells your particular instance of NSWindow to get its contentView which happens to be the instance of CutoutView you 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 contentView and 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 rdelmar asked in the comment way up there, the instance of 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/boat doesn'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:

    这篇关于从已经初始化的其他类调用NSView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆