Pregunta

Beginnersquestion:

Have three buttons representing letters a, e and i. When the buttons are pressed, corresponding letters should show up in a label. So when each button is pressed once, the label would say "aei". Attached my code below. When pressing the buttons three times now, the label only shows the last pressed button letter. What am i doing wrong? Thank you for your help!


#import "SecondViewController.h"


NSMutableString *stringLabel;


@interface SecondViewController ()
@end

@implementation SecondViewController




-(IBAction)type_a:(id)sender;{
[stringLabel appendString: @"a"];
NSLog(@"stringLabel is set to: %@", stringLabel);
label.text = stringLabel;


}

-(IBAction)type_e:(id)sender;{
[stringLabel appendString: @"e"];
NSLog(@"stringLabel is set to: %@", stringLabel);
label.text = stringLabel;


}

-(IBAction)type_i:(id)sender;{
[stringLabel appendString: @"i"];
NSLog(@"stringLabel is set to: %@", stringLabel);
label.text = stringLabel;


}
¿Fue útil?

Solución

I don't know where you are initialising your NSMutableString but maybe you are missing this:

stringLabel = [[NSMutableString alloc] init];

Put that code in viewDidLoad and it should works because your code is right.

Also, where are you setting the connection between the label and your viewController? I can't see the

@property (weak, nonatomic) IBOutlet UILabel *label;

Check that too

Otros consejos

In your post each time You are assigning value to label using

    label.text = stringValue

assigns the new value by erasing the old value

where as you have to concatenate the old string with new stringValue..

store previous value of Label in a NSMutableString like

    NSMutableString *previosValue = label.text;

now concatenate the buttonValue to previousValue and add to label

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top