2013-10-28 36 views
0

Beginnersquestion:NSMutableString append string

有三个按钮代表字母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; 


} 

回答

0

我不知道你在哪里初始化您的NSMutableString,但也许你缺少这样的:

stringLabel = [[NSMutableString alloc] init]; 

把这些代码在viewDidLoad,它应该可以工作,因为你的代码是正确的。

另外,你在哪里设置标签和viewController之间的连接?我看不到

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

检查过

+0

谢谢你,我的标签显示连接错误。 再次进行连接,现在它工作! 再次感谢! – user2928201

0

在您的文章,每次要指定值使用

label.text = stringValue 

标记通过擦除旧值

这里你有来连接旧分配的新值字符串与新的stringValue ..

将标签的以前的值存储在一个NSMutableString像

NSMutableString *previosValue = label.text; 

现在串联的buttonValue到PREVIOUSVALUE并添加标记