分配的可可子类对象的实例变量
我尝试过为 NSButton 的子类声明几个长变量的大量变体;基本上,当创建对象时,我想在其中粘贴几个值,以便我以编程方式创建的按钮可以工作...
pool = [[NSAutoreleasePool alloc] init];
...
scbi = [[[MyNSButtonSubClass alloc] initWithFrame: buttonInitRect]
autorelease];
[scbi setButtonid: myLongButtonId];
[scbi setTarget: self];
[scbi setAction: @selector(buttonAction:];
...
[pool release];
将与此一起工作:
- (IBAction)buttonAction:(id)sender
{
MyNSButtonSubclass *snbi;
long buttonid;
snbi = (MyNSButtonSubclass *)sender
buttonid = [snbi buttonid];
}
在子类中,我有:
- (void)setButtonid:(long)x
{
buttonid = x;
}
...我尝试声明按钮ID long 在全局范围和头文件中的接口结构中,我尝试使用属性,删除了 setter 和 getter 并尝试了 @synthesize ...并且我检查了 set 调用,它们正在接收各种长 id ...但无论我做什么,buttonAction 方法都会从 [snbi buttonid] 返回零;无论按下哪个按钮(可以有数百个,数量各不相同。我必须知道按下了哪个按钮,并且我无法为它们编写单独的目标例程,因为我不知道会有多少个) )
看来向子类添加实例变量的需要是如此明显,并且访问它们的机制如此基本,以至于没有人谈论过它。我找不到任何可以产生工作代码的信息。 Cocoa 文档提到了它,但没有演示它,或者至少我可以找到它。
有什么见解吗?
I have tried a very large number of variations of declaring a couple of long variables for a subclass of NSButton; basically, when the object is created, I want to stick a couple of values in there so my programattically created buttons will work...
pool = [[NSAutoreleasePool alloc] init];
...
scbi = [[[MyNSButtonSubClass alloc] initWithFrame: buttonInitRect]
autorelease];
[scbi setButtonid: myLongButtonId];
[scbi setTarget: self];
[scbi setAction: @selector(buttonAction:];
...
[pool release];
will work with this:
- (IBAction)buttonAction:(id)sender
{
MyNSButtonSubclass *snbi;
long buttonid;
snbi = (MyNSButtonSubclass *)sender
buttonid = [snbi buttonid];
}
in the subclass, I have:
- (void)setButtonid:(long)x
{
buttonid = x;
}
...I've tried declaring the buttonid long in the global scope and in the interface structure in the header file, I've tried using properties, removed the setter and getter and tried @synthesize... and I've checked the set calls, they are receiving the various long ids... but no matter what I do, the buttonAction method gets back zeros from [snbi buttonid]; no matter which button is pressed (there can be hundreds, and the number varies. I have to know which button was pressed, and I can't write a separate target routine for them, because I don't know how many there will be at any one time.)
It appears that the need for adding instance variables to a subclass is be so obvious, and the mechanism to access them so basic, that no one ever talks about it. I can't find anything about it that leads to working code. The Cocoa docs mention it, but don't demonstrate it, or at least that I could find.
Any insight, anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论