2013-08-21 32 views
0

我一直在嘗試通過大量搜索和觀看視頻來解決過去幾個小時這個問題。這是我想要做的:NSOutlineView將不會重新加載keydown上的數據,但重點更新

摘要:一個簡單的窗口允許我將郵件從我的收件箱移動到我的許多郵件文件夾中的任意一個。當我輸入我正在查找的郵件文件夾的名稱時,大綱視圖應自動刷新,只顯示帶有匹配文本的郵件文件夾 - 如果知道它,請考慮MsgFiler。

  • 在文本框中
  • 當我輸入每個字母鍵入文字,我想NSOutlineView更新它的數據有符合我輸入的文字越來越少的文件夾。 (此刻,我的測試只是增加了一個 「測試郵箱」 進入NSOutlineView

當前結果

  • 在文本框中鍵入文本
  • 什麼也沒有發生
  • 點擊NSOutlineView ,視圖得到更新

TextfieldDelegate.m:

#import "TextFieldDelegate.h" 
#import "OutlineViewController.h" 
#import "GetMailDatasource.h" 

@implementation TextFieldDelegate 

@synthesize testLabel; 

- (void) controlTextDidChange :(NSNotification *) sender { 
    NSTextField *changedField = [sender object]; 
    NSLog(@"in control text did change"); 

    //Just some text code to see the change when text does change 
    NSString *text = [changedField stringValue]; 
    [testLabel setStringValue:text]; 

    NSLog(@"changed the label and creating the data now"); 

    OutlineViewController *vc = [[OutlineViewController alloc] init]; 
    [vc refreshTheData:sender]; 
    [vc.outlineView reloadData]; 
    [vc release]; 
} 

- (void)controlTextDidEndEditing:(NSNotification *)obj { 
    NSLog(@"in end editting"); 
    return; 
} 

@end 

OutlineViewController.m

#import "OutlineViewController.h" 
#import "GetMailDatasource.h" 

@implementation OutlineViewController 

- (id) init { 

    self = [super init]; 
    if (self) { 
    _mailboxes = [[NSMutableArray alloc] init]; 
    if (myMailboxes != nil) { 
     _mailboxes = myMailboxes; 
    } else { 
     GetMailDatasource *mailDatasource = [[GetMailDatasource alloc] init]; 
     [mailDatasource createFakeData]; 
     _mailboxes = myMailboxes; 
    } 
    } 

    NSLog(@"inited outline view controller"); 
    return self; 
} 

- (IBAction) refreshTheData : (id) sender { 

    NSLog(@"in refreshTheData"); 

    Mailbox *m = [self.outlineView itemAtRow:[self.outlineView selectedRow]]; 
    if (m) 
    [m addChild:[[Mailbox alloc] init]]; 
    else 
     [self.mailboxes addObject:[[Mailbox alloc] init]]; 

    NSLog(@"running reloadData"); 
    [self.outlineView reloadData]; 
} 

#I know these work, but providing them for completeness 
#pragma mark NSOutlineView Data Source Methods 

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { 
    return !item ? [self.mailboxes count] : [[ item children] count ]; 
} 

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { 
    return !item ? YES : [[item children ] count] != 0; 
} 

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { 
    return !item ? [self.mailboxes objectAtIndex:index] : [[item children] objectAtIndex:index]; 
} 

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { 
    return [item name]; 
} 

@end 

我有,我在界面上添加一個單獨的「測試按鈕」只是嘗試,我已經束縛它的「已發送行動」直接到「refreshTheData 「在OutlineViewController上的功能,並且工作順利。在NSOutlineView中添加新項目並立即更新。

我不知道還有什麼是必要的,試圖找出問題所在......任何建議將不勝感激!

非常感謝!

回答

0

您每次創建一個新實例controlTextDidChange。所以你改變這個OutlineViewController的設置,並釋放它。

取而代之,您應該在屏幕上使用大綱視圖。

+0

我該如何在屏幕上使用大綱視圖?我已經嘗試過不同的方式來做到這一點,但是當我在TextFieldDelegate.h中爲NSOutlineView創建屬性並嘗試執行類似[outlineView refreshTheData]的操作時,我收到消息「outlineView可能無法響應refreshTheData」,任何想法? –

+0

只需添加,在我的TextFieldDelegate.h中,我引用屬性如下: –

+0

@property(retain)IBOutlet NSOutlineView * outlineView; (儘快輸入) –

0

解決了它。

因此,我已經在IB中提供了Outline視圖,並且在IB中已經有了OutlineViewController的對象。我什至嘗試通過使用我已經關聯到我的TextFieldDelegate的OutlineView對象在我的控制器中引用新方法「refreshTheData」。那是我錯了的地方。

我需要做的是將OutlineViewController本身關聯到我的TextFieldDelegate,然後我可以用IB接口中的相應鏈接調用「refreshTheData」。

所以,最後,代碼如下所示,改變強調這樣的 - >:

TextFieldDelegate。^ h

#import <Foundation/Foundation.h> 
--> #import "OutlineViewController.h" 

@interface TextFieldDelegate : NSObject { 
    IBOutlet OutlineViewController *outlineView; 
} 

@property (nonatomic, strong) IBOutlet NSTextField * testLabel; 
@property (nonatomic, strong) IBOutlet NSTextField * userInput; 

- (IBAction) controlTextDidChange :(id) sender; 

@end

而這現在允許TextFieldDelegate.m的代碼,得到認可:

- (void) controlTextDidChange :(NSNotification *) sender { 
    NSTextField *changedField = [sender object]; 

    NSString *text = [changedField stringValue]; 
    [testLabel setStringValue:text]; 

    --> [outlineView refreshTheData:sender]; 
} 

的突出的行始終認爲,因爲我想它可能無法識別方法在實際的NSOutlineView項目上使用它,而不是它創建該方法的委託。我曾經想過,通過將代理連接到NSOutlineView,它們將變成同一個,或者可以通過傳遞性來查找該方法。

感謝指針@Amin,它幫助我到達那裏。