我試圖以編程方式生成一組類似的控件並將事件綁定到它們。事件邏輯將相對於幾個控件屬性創建。所以,我寫了這樣的:如何獲取父控件屬性?
// 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)屬性?
據我所知,根據this question和this one我不能直接傳遞setAction參數。如何解決這個問題?
更新時間:
所以,我在DiclosureControl_Click
事件犯了一個錯誤,認爲發送方將FolderButton類型,這只是一個單一的按鈕。我已經改寫和更新了這個問題。
你能不能只需將「FolderControl」的引用保留爲實例變量,而不是創建它並將其添加到視圖;所以'_folderControl = [[FolderControl alloc] initWithNibName:...'? – Droppy 2014-10-17 07:55:32