程序日志--ios“迷你秒表”程序
2015-07-28 14:55
726 查看
交代一下我做程序的工具:mac os x虚拟机10.9.3 Xcode6 百度^-^ 参考书iPhone30天精通
程序介绍:做一个迷你秒表的程序,有开始计时,暂停计时,重新计时(将计时的时间归零)
在按照书上的程序做出来之后发现一个问题,就是多次按开始计时,计时的时间就加快了,即重复调用开始计时的方法,后来上网上查了一下,很多人都有遇到相同的问题,我便借鉴了一下别人的博客,利用了两个条件语句解决了这一问题,参考:http://sinno.blog.163.com/blog/static/2156171902013092625208/ 完全可以看下面的博文不用看参考资料。
程序贴出来,同时注解在程序上面。
// ViewController.h
// 10
//
// Created by 李迪 on 15-7-27.
// Copyright (c) 2015年 李迪. All rights reserved.
//
#import <UIKit/UIKit.h>
#import<Foundation/Foundation.h>
@interface ViewController : UIViewController{
IBOutlet UILabel * time;
NSTimer * timer;
}
@property UILabel *time;
@property NSTimer *timer;
-(IBAction)start;
-(IBAction)stop;
-(IBAction)reset;
-(void)showAct;
@end
// ViewController.m
// 10
//
// Created by 李迪 on 15-7-27.
// Copyright (c) 2015年 李迪. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize time,timer;
-(IBAction)start{//计时器当然是要从0开始查了,我在viewDidLoad里面让label.text初始值为0,在reset里也让它的值为0
if(!timer)//在.h文件里面只是定义了timer这一属性,但是并没有给timer这一属性赋值,所以timer是一个空值的计时器对象。如果timer
//对象为空成立,则创建一个相应的timer对象,否则什么也不做。这样就保证了多次按start也不发生加速计时的情况了。
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showAct) userInfo:nil repeats:YES];//scheduledTimerWithTimeInterval为对象定义计时的时间间隔属性值,1.0表示1秒
//target定义对象执行的目标,“self”代表本体
//selector为对象定义时间选择的方法,而showAct是自己定义的一个方法,将数字累加,让结果在label上显示出来
//userInfo是用户信息,不需要管
//repeats表示定时器是否重复执行,一定要选yes,否则定时器便只查1秒就不再继续查了
}
-(IBAction)stop{
if(timer){//这里的if是和start的if相互呼应的,如果没按start直接按stop便没有意义
if([timer isValid]){//如果上一个if有意义的话,判断程序本身的timer是否在运行,不运行也同样没有意义
[timer invalidate];//终止计时器方法
timer = nil;//与创建一个start相呼应,我在这里让这个对象为空
}
}
}
-(IBAction)reset{
time.text = @"0";
}
-(void)showAct{//前面所调用的方法,即累加器+显示器
int currentTime = [time.text intValue];
int newTime = currentTime +1;
time.text = [NSString stringWithFormat:@"%d",newTime];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
time.text = @"0";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
程序介绍:做一个迷你秒表的程序,有开始计时,暂停计时,重新计时(将计时的时间归零)
在按照书上的程序做出来之后发现一个问题,就是多次按开始计时,计时的时间就加快了,即重复调用开始计时的方法,后来上网上查了一下,很多人都有遇到相同的问题,我便借鉴了一下别人的博客,利用了两个条件语句解决了这一问题,参考:http://sinno.blog.163.com/blog/static/2156171902013092625208/ 完全可以看下面的博文不用看参考资料。
程序贴出来,同时注解在程序上面。
// ViewController.h
// 10
//
// Created by 李迪 on 15-7-27.
// Copyright (c) 2015年 李迪. All rights reserved.
//
#import <UIKit/UIKit.h>
#import<Foundation/Foundation.h>
@interface ViewController : UIViewController{
IBOutlet UILabel * time;
NSTimer * timer;
}
@property UILabel *time;
@property NSTimer *timer;
-(IBAction)start;
-(IBAction)stop;
-(IBAction)reset;
-(void)showAct;
@end
// ViewController.m
// 10
//
// Created by 李迪 on 15-7-27.
// Copyright (c) 2015年 李迪. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize time,timer;
-(IBAction)start{//计时器当然是要从0开始查了,我在viewDidLoad里面让label.text初始值为0,在reset里也让它的值为0
if(!timer)//在.h文件里面只是定义了timer这一属性,但是并没有给timer这一属性赋值,所以timer是一个空值的计时器对象。如果timer
//对象为空成立,则创建一个相应的timer对象,否则什么也不做。这样就保证了多次按start也不发生加速计时的情况了。
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showAct) userInfo:nil repeats:YES];//scheduledTimerWithTimeInterval为对象定义计时的时间间隔属性值,1.0表示1秒
//target定义对象执行的目标,“self”代表本体
//selector为对象定义时间选择的方法,而showAct是自己定义的一个方法,将数字累加,让结果在label上显示出来
//userInfo是用户信息,不需要管
//repeats表示定时器是否重复执行,一定要选yes,否则定时器便只查1秒就不再继续查了
}
-(IBAction)stop{
if(timer){//这里的if是和start的if相互呼应的,如果没按start直接按stop便没有意义
if([timer isValid]){//如果上一个if有意义的话,判断程序本身的timer是否在运行,不运行也同样没有意义
[timer invalidate];//终止计时器方法
timer = nil;//与创建一个start相呼应,我在这里让这个对象为空
}
}
}
-(IBAction)reset{
time.text = @"0";
}
-(void)showAct{//前面所调用的方法,即累加器+显示器
int currentTime = [time.text intValue];
int newTime = currentTime +1;
time.text = [NSString stringWithFormat:@"%d",newTime];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
time.text = @"0";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS 设计模式系列:Facade – 外观模式
- iOS 设计模式系列:Singleton – 单例模式
- iOS 设计模式系列:开篇
- iOS 设计模式系列:MVC – 设计模式中的国王
- How to Make an iOS VoIP App With Pjsip: Part 5
- iOS KVC详细讲解(刚刚)
- How to Make an iOS VoIP App With Pjsip: Part 1
- How to Make an iOS VoIP App With Pjsip: Part 3
- How to Make an iOS VoIP App With Pjsip: Part 4
- How to Make an iOS VoIP App With Pjsip: Part 2
- iOS KVO概述(刚刚)
- iOS 发布证书申请
- iOS多线程的初步研究(三)-- NSRunLoop
- iOS协议里面可以定义成员变量和属性吗?(刚刚)
- iOS下载图片并显示进度
- iOS开发-优秀博客-实用技巧
- iOS设置导航与其标题的颜色及字体大小和系统默认TabBar的相关设置与使用方法
- iOS开发的一些奇巧淫技
- iOS地图定位使用几个小功能(刚刚)
- 【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识