創建一個 Timer
- + scheduledTimerWithTimeInterval: invocation: repeats:
- + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
- + scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:
- + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
- 創建返回一個新的NSTimer對象和時間表,在當前的默認模式下循環調用一個實例方法。
- + timerWithTimeInterval: invocation: repeats:
- + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
- + timerWithTimeInterval: target:selector: userInfo:repeats:
- + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
- – initWithFireDate: interval: target: selector: userInfo: repeats:
- - (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;
scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
預訂一個Timer,設置一個時間間隔。
表示輸入一個時間間隔對象,以秒為單位,一個>0的浮點類型的值,如果該值<0,系統會默認為0.1
target:(id)aTarget
表示發送的對象,如self
selector:(SEL)aSelector
方法選擇器,在時間間隔內,選擇調用一個實例方法
userInfo:(id)userInfo
此參數可以為nil,當定時器失效時,由你指定的對象保留和釋放該定時器。
repeats:(BOOL)yesOrNo
當YES時,定時器會不斷循環直至失效或被釋放,當NO時,定時器會循環發送一次就失效。
invocation:(NSInvocation *)invocation
啟動 Timer
- – fire
停止 Timer
- – invalidate
Timer設置
- – isValid
- – fireDate
- – setFireDate:
- – timeInterval
- – userInfo
NSTimeInterval類:是一個浮點數字,用來定義秒
例子:
iphone為我們提供了一個很強大得時間定時器 NSTimer
他可以完成任何定時功能:
我們使用起來也很簡單,只要記住三要素就可以,具體得三要素是:時間間隔NSTimeInterval浮點型,事件代理
delegate和事件處理方法@selector();就可以用
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo; 來初始化一個 時間定時器
下面我寫了一個很簡單得例子
初始化一個定時器:
-(void)initTimer
{
//時間間隔
NSTimeInterval timeInterval =1.0 ;
//定時器
NSTimer showTimer = [NSTimer scheduledTimerWithTimeInterval:maxShowTime
target:self
selector:@selector(handleMaxShowTimer:)
userInfo:nil
repeats:NO];
}
//觸發事件
-(void)handleMaxShowTimer:(NSTimer *)theTimer
{
NSDateFormatter dateFormator = [[NSDateFormatter alloc] init];
dateFormator.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *date = [dateformater stringFromDate:[NSDate date]];
if([date isEqualToString:@"2011-11-09 23:59:59"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TITLE_NAME
message:@"現在馬上就有新的一天了!"
delegate:self
ancelButtonTitle:nil
otherButtonTitles:CONFIRM_TITLE, nil];
[alert show];
[alert release];
}
[data release];
[dateFormator release];