3

I have some 10 buttons with different names each.On selecting each button I need to append the button's title to NSTextField without removing the older string.

I have tried in the following way.

- (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]];
    }
}

Here resultField is my NSTextField.

setStringValue overriding the new string,so that I couldnot able to append string to NSTextField.Is there any easy way to implement this or else use NSString value to hold the previous string and set this string to NSTextFiled along with new button's string value.

Akbar
  • 1,509
  • 1
  • 16
  • 32

2 Answers2

6

How about something like:

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

The idea here is that you take the original content of your NSTextField and compose a new string via stringWithFormat and then assign that new string to your NSTextField.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
3

Use stringByAppendingString: or stringWithFormat: to join two strings together:

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

or:

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

Choose stringByAppendingString: if you really want a basic append, or stringWithFormat: if you need whitespace/etc in between.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110