2012-03-27 99 views
3

我有10个按钮,每个按钮的名称都不相同。在选择每个按钮时,我需要将按钮的标题附加到NSTextField,而不删除旧字符串。NSTextField setStringValue:将字符串附加到NSTextField

我尝试过以下方法。

- (IBAction)nextButton:(NSButton*)sender 
{ 
    int tag = [sender tag]; 

    if (tag==1) { 

     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==2) { 

     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==3) { 

     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==4) { 
     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==5) { 
     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==6) { 

     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==7) { 

     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==8) { 

     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==9) { 

     [resultField setStringValue:[sender title]]; 
    } 
    else if (tag==10) { 

     [resultField setStringValue:[sender title]]; 
    } 
} 

这里resultField是我的NSTextField。

setStringValue重写新的字符串,这样我couldnot能够追加字符串NSTextField.Is有实现这个没有简单的方法或者使用的NSString值来保存以前的字符串,并与新的按钮的字符串集合沿此字符串NSTextFiled值。

回答

6

如何像:

[resultField setStringValue: 
    [NSString stringWithFormat: @"%@ %@", [resultField stringValue], [sender title]]; 

这里的想法是,你把你的NSTextField的原创内容,并通过stringWithFormat组成一个新的字符串,然后分配一个新的字符串到您的NSTextField。

3

使用stringByAppendingString:stringWithFormat:加入两个字符串:

resultField.stringValue = [resultField.stringValue stringByAppendingString:sender.title]; 

或:

resultField.stringValue = [NSString stringWithFormat:@"%@ %@", resultField.stringValue, sender.title]; 

选择stringByAppendingString:,如果你真的想要一个基本的追加,或stringWithFormat:如果你需要空白的/ etc之间。