主视图控制器类头文件
#import <UIKit/UIKit.h>
//UITextField与文本框相关的方法
@interface MainViewController : UIViewController<UITextFieldDelegate>
{
int flag;
NSTimer* time;
int miniSeconds;
int seconds;
int minites;
}
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet UILabel *showTime;
- (IBAction)Start:(id)sender;
- (IBAction)reset:(id)sender;
@end
//主视图控制器类实现文件
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
// [btn setFrame:CGRectMake(100, 300, 200, 44)];
// [btn setTitle:@"别点击我,否则揍你。" forState:UIControlStateNormal];
// [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:btn];
flag=0;
time=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(CalTime) userInfo:nil repeats:YES];
[time setFireDate:[NSDate distantFuture]];
miniSeconds = 0;
seconds = 0;
minites = 0;
[self.showTime setText:@"00:00:00"];
[self.showTime setTextColor:[UIColor redColor] ];
// Do any additional setup after loading the view from its nib.
// [self initDate];
}
//当前视图控制器的对象为文本框的代理
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];//消除文本框的第一响应者
return YES;
}
-(void)initDate
{
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 200, 30)];
field.placeholder = @"请输入用户名";//设置占位符
field.borderStyle = UITextBorderStyleLine;//设置文本框的边框
field.textColor = [UIColor redColor];
field.delegate=self;//设置文本框的代理
[self.view addSubview:field];
//键盘文本的第一响应这
}
-(void)CalTime
{
miniSeconds++;
if (miniSeconds==100) {
seconds++;
if (seconds==60) {
minites++;
seconds=0;
}
miniSeconds=0;
}
NSLog(@"%@", [NSString stringWithFormat:@"%02d:%02d:%02d", minites, seconds, miniSeconds]);
self.showTime.text=[NSString stringWithFormat:@"%02d:%02d:%02d", minites, seconds, miniSeconds];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)click
{
NSLog(@"好吧!, 你强我不揍你了");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)Start:(id)sender {
if (flag==0) {
[self.startBtn setTitle:@"停止" forState:UIControlStateNormal];
flag=1;
[time setFireDate:[NSDate date]];
}
else if (flag==1)
{
[self.startBtn setTitle:@"启动" forState:UIControlStateNormal];
flag=0;
[time setFireDate:[NSDate distantFuture]];
}
}
- (IBAction)reset:(id)sender {
if (flag == 0) {
miniSeconds = 0;
seconds = 0;
minites = 0;
self.showTime.text=[NSString stringWithFormat:@"%02d:%02d:%02d", minites, seconds, miniSeconds];
}
}
@end
效果: