NSMutableString追加字符串

时间:2013-10-28 13:24:16

标签: nsmutablestring

Beginnersquestion:

有三个按钮代表字母a,e和i。 按下按钮时,相应的字母应显示在标签中。 因此,当按下每个按钮一次时,标签会显示“aei”。 附上我的代码如下。现在按三次按钮,标签 仅显示最后按下的按钮字母。 我究竟做错了什么? 谢谢你的帮助!


#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;


}

2 个答案:

答案 0 :(得分:0)

每次使用

为标签指定值时,在帖子中
    label.text = stringValue

通过删除旧值

来指定新值

你必须将旧字符串与新的stringValue连接起来。

将Label的先前值存储在NSMutableString中,如

    NSMutableString *previosValue = label.text;

现在将buttonValue连接到previousValue并添加到标签

答案 1 :(得分:0)

我不知道你在哪里初始化你的NSMutableString,但也许你错过了这个:

stringLabel = [[NSMutableString alloc] init];

将该代码放在viewDidLoad中,它应该有效,因为你的代码是正确的。

此外,您在哪里设置标签和viewController之间的连接?

我看不到
@property (weak, nonatomic) IBOutlet UILabel *label;

检查一下

相关问题