NSTimer *timer; timer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(setCommand) userInfo:nil repeats:YES]; - (void) setCommand { }
[timer invalidate];
// // ViewController.h // timerApp // // Created by Weerachai on 12/8/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { IBOutlet UILabel *lblResult; NSTimer *timer; } - (IBAction)btnStart:(id)sender; - (IBAction)btnStop:(id)sender; @end
// // ViewController.m // timerApp // // Created by Weerachai on 12/8/55 BE. // Copyright (c) 2555 Weerachai. All rights reserved. // #import "ViewController.h" @interface ViewController () { int i; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)btnStart:(id)sender { timer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(setCommand) userInfo:nil repeats:YES]; } - (void) setCommand { i = i + 1; NSMutableString *result; result = [NSString stringWithFormat:@"Number = %d",i]; lblResult.text = result; } - (IBAction)btnStop:(id)sender { [timer invalidate]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [lblResult release]; [super dealloc]; } @end
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท