这一章就是设计了一个计数器,涉及整数数据转换成文字格式数据的方法。
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
count = 0;
//用于取得文字数据类型,把整数数据转换为文字格式数据
NSString *numValue = [NSString stringWithFormat:@"%d", count];
//数字显示标签的文字替换为字符变量numValue中的数据内容
counter.text = numValue;
}
- (IBAction)addUnit {
if(count <= 999){
count++;
}else {
count=999;
}
//创建一个字符变量numValue,用于取得文字数据类型,把整数数据转换为文字格式数据
NSString *numValue = [NSString stringWithFormat:@"%d", count];
//数字显示标签的文字替换为字符变量numValue中的数据内容
counter.text = numValue;
}
//减法
- (IBAction)subtractUnit {
if(count >0){
count--;
}else {
count=0;
}
NSString *numValue = [NSString stringWithFormat:@"%d", count];
counter.text = numValue;
}
//重置
-(IBAction)reset{
count = 0;
NSString *numValue = [NSString stringWithFormat:@"%d", count];
counter.text = numValue;
}