2012-03-27 7 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私はそこNSTextField.Isにこれを実装するための簡単な方法を文字列を追加したり、他の前の文字列を保持するためにNSStringの値を使用して、新しいボタンの文字列と一緒にNSTextFiledにこの文字列を設定することができcouldnotように、新しい文字列をオーバーライド値。

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

ここでの考え方は、あなたのNSTextFieldの元のコンテンツを取るとstringWithFormatを経由して、新しい文字列を構成して、あなたのNSTextFieldにその新しい文字列を割り当てることです:のようなものについてはどのように

答えて

6

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

か::二つの文字列一緒に参加する

3

使用stringByAppendingString:またはstringWithFormat:

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

をあなたが間になど/空白が必要な場合は、あなたが本当に基本的なAPPEND、またはstringWithFormat:をしたい場合stringByAppendingString:を選択します。 。

関連する問題