2012-04-27 51 views
0

我只是想「輸入」在字段中放置「NewLine」字符,以便我可以繼續輸入。 THX當我按Enter鍵時,我的NSTextField突出顯示文本。我只想「輸入」在該字段中放置「NewLine」字符,以便我可以繼續輸入

- (void)someAction:(id)sender 
{ 
    NSString *s = [tf1 stringValue]; 
    [tf1 setStringValue:[NSString stringWithFormat:@"%@%@",s, @"\nEnter\n" ]]; 
} 

- (void)myRun:(NSButton*)btn 
{ 
    NSString *s = [tf1 stringValue]; 
    [tf1 setStringValue:[NSString stringWithFormat:@"%@%@",s, @"hello\nthere" ]]; 
} 


- (void)windowControllerDidLoadNib:(NSWindowController *)aController 
{ 
    [super windowControllerDidLoadNib:aController]; 
    // Add any code here that needs to be executed once the windowController has loaded the document's window. 

    //[aController.window setFrame:CGRectMake(10,10,1000,600) display:TRUE]; 
    [aController.window setFrame:NSMakeRect(10,10,1000,600) display:TRUE]; 
    [aController.window setBackgroundColor:[NSColor grayColor]]; 



    //NSButton *b1 = [[NSButton alloc] initWithFrame:CGRectMake(10, 530, 100, 30)]; 
    NSButton *b1 = [[NSButton alloc] initWithFrame:NSMakeRect(10, 530, 100, 30)]; 
    [b1 setTitle:@"Run"]; 
    [b1 setAction:@selector(myRun:)]; 
    [aController.window.contentView addSubview:b1]; 

    tf1 = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 600, 500)]; 
    [aController.window.contentView addSubview:tf1]; 
    [tf1 setTarget:self]; 
    [tf1 setAction:@selector(someAction:)]; 

} 
+0

我如果有一個簡單的方法,使工作的NSTextField不同不確定,你可以使用一個NSTextView你想要的東西...... – 2012-04-27 21:28:39

+0

我試圖做一個命令編輯器響應文本,但移動到下一行,所以我可以查詢另一個命令... thx – jdl 2012-04-27 21:30:24

+0

嘗試攔截委託方法,如... DidEndEditing ... – 2012-04-27 21:46:20

回答

1
+0

我該如何實現代碼... @interface Document:NSDocument < NSTextFieldDelegate>? – jdl 2012-04-27 22:35:34

+0

你在問如何讓你的班級成爲文本字段的代表?是的,代表必須採用'NSTextFieldDelegate'協議,並且您編寫的'@interface'行爲'Document'類做了這些。你必須在代碼中連接文本字段的* delegate * outlet或調用'-setDelegate:'。 – 2012-04-27 22:53:48

相關問題