Objective c 如何获取父控件属性?
我试图以编程方式生成一组类似的控件,并将事件绑定到它们。事件逻辑将相对于多个控件属性创建。所以,我写了这个:Objective c 如何获取父控件属性?,objective-c,macos,cocoa,Objective C,Macos,Cocoa,我试图以编程方式生成一组类似的控件,并将事件绑定到它们。事件逻辑将相对于多个控件属性创建。所以,我写了这个: // Create zero root folder controls for (SubFolderType* subFolder in rootFolder.subFolders) { FolderControl* folderControl = [[FolderControl alloc] initWithNibName:@"FolderControl" bu
// Create zero root folder controls
for (SubFolderType* subFolder in rootFolder.subFolders)
{
FolderControl* folderControl = [[FolderControl alloc] initWithNibName:@"FolderControl" bundle:nil type:subFolder.type title:subFolder.name rootLevel:0];
// positioning
[folderControl view].frame = CGRectMake(30, 200 - iterator*40, 250, 40);
[folderControl view].wantsLayer = YES;
[[folderControl view] setShadow:shadow];
[folderControl view].layer.backgroundColor = [[NSColor whiteColor] CGColor];
// bind to self and add action
[folderControl.DiclosureControl setTarget:self];
[folderControl.DiclosureControl setAction:@selector(DiclosureControl_Click:)];
// add to view as subview
[dialogControl.view addSubview:[folderControl view]];
iterator++;
}
FolderControl是一个自定义控件,它有几个属性,如title或rootLevel。该事件绑定到披露三角形,该三角形应生成下一个根级别的控件
所以,它看起来像:
- (IBAction) DiclosureControl_Click : (NSButton*) sender {
// How to get folderControl properties here?
NSView *parentView = [sender superview]; // That's a NSView object, how can I get FolderControl type, which is NSViewController?
// generate the next level of controls
}
问题是,发送方是一个双闭合按钮,它是一个简单的NSButton。如何在此处获取父控件(FolderControl)属性
我理解,根据,我不能用setAction直接传递参数。如何解决这个问题
更新:
所以,我在
DiclosureControl\u Click
事件中犯了一个错误,认为发送方应该是FolderButton类型,而它只是一个按钮。我已重新表述并更新了问题。请确保如果您不将FolderControl
实例存储在任何位置,它们将被释放,因为视图没有对视图控制器的强引用
因此,请更改您的代码,这样您不仅可以:
[dialogControl.view addSubview:[folderControl view]];
但某种程度上:
[dialogControl.folderControllers addObject:folderControl];
通过这种方式,您可以:
folderController.DiclosureControl.tag = [dialogControl.folderControllers indexOfObject:folderControl];
...
- (IBAction) DiclosureControl_Click : (NSButton*) sender {
FolderController *controller = dialogControl.folderControllers[sender tag];
...
}
您不能将对
FolderControl
的引用保留为实例变量,而不是创建它并将其添加到视图中;所以\u folderControl=[[folderControl alloc]initWithNibName:…
?是的,我知道了基本的想法。谢谢。