我怎么知道哪个对象正在向委托人索取信息

我怎么知道哪个对象正在向委托人索取信息

本文介绍了我怎么知道哪个对象正在向委托人索取信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的设计中,选择器告诉应用程序控制器用户希望在两个视图中的每个视图中看到多少个项目以及哪种颜色.我有一个NSView对象描述(myView.h,myView.m),该控制器实例化了两次.当用户调整选择器以更改要在viewOne中绘制的项目数时,App Controller会为此数量更新其局部变量,并告诉viewOne重绘自身.作为重绘本身的一部分,viewOne需要询问委托人(应用程序控制器)要绘制多少个项目.当通过协议发生这种情况时,委托人通过什么机制知道要求viewty变量的是viewOne?,即委托人如何知道响应哪个变量?

In my design, choosers tell an app controller how many items, and of which color, the user would like to see in each of two Views. I have a single NSView object description (myView.h, myView.m) which the controller instantiates twice. When the user adjusts a chooser to change, say, the number of items to be drawn in viewOne, the App Controller updates its local variable for this qty, and tells viewOne to redraw itself. As part of redrawing itself, viewOne needs to ask the delegate (App Controller) how many items to draw. When this happens via the protocol, by what mechanism does the delegate know that it is viewOne who is asking for the qty variable? i.e. how does the delegate know which variable to respond with?

推荐答案

我发现我可以(不确定是否应该)在委托人的例程中使用isEqual:来确定哪个对象称为:

I figured out that I can (not sure if I should) use isEqual: in the delegate's routine, to determine which object called:

- (int)numberOfItemsInTheView:(myView *)aView {
    int itemCount = 0;

    if([aView isEqual:[self viewOne]]) {
        itemCount = 5;
    } else if([aView isEqual:[self viewTwo]]) {
        itemCount = 18;
    }
    return itemCount;
}

这是我不了解或不了解的isEqual部分/陈述.关于Josh的回答,我可以看到我可能不完全了解协议/委托编码结构的含义和/或要求(约定?).

It is the isEqual portion/statement that I was not aware of or understanding. Regarding Josh's answer, I can see that I may not fully understand the implications and/or requirements (conventions?) of the protocol/delegate coding structure.

这篇关于我怎么知道哪个对象正在向委托人索取信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:33