Reputation: 1139
I'm trying to generate programmatically a group of similar controls and bind events to them. The event logic would be created relatively to several control properties. So, I've written this:
// 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 is a custom control and it has several properties, like title, or rootLevel. The event is binded to disclosure triangle, that should generate the next root level of controls.
So, it looks like:
- (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
}
Problem is, sender is a diclosure button which is a simple NSButton. How can I get parent control (FolderControl) properties here?
I understand, that according to this question and this one I can't pass directly parameters with setAction. How to solve this issue?
Updated:
So, I made a mistake in DiclosureControl_Click
event, thought that sender would be FolderButton type, and it was just a single button. I've rephrased and updated the question.
Upvotes: 0
Views: 352
Reputation: 1471
Preatty sure that if you don't store the FolderControl
instances anywhere, they will be released as the view has no strong reference to the view controller.
So change your code so that you not only do :
[dialogControl.view addSubview:[folderControl view]];
but some sort or :
[dialogControl.folderControllers addObject:folderControl];
This way you can do:
folderController.DiclosureControl.tag = [dialogControl.folderControllers indexOfObject:folderControl];
...
- (IBAction) DiclosureControl_Click : (NSButton*) sender {
FolderController *controller = dialogControl.folderControllers[sender tag];
...
}
Upvotes: 1