閱讀840 返回首頁    go 阿裏雲 go 技術社區[雲棲]


在ios下定時任務的小例子

在ios下定時任務的小例子

為了模擬翻頁場景(為了測試內存是否泄露),需要用定時任務刷新界麵。寫了個小例子。

image

一上來是空白頁,3秒鍾後顯示第1頁,以後間隔3秒,頁號增加,直到第5頁。

首先,在controlller頭文件中增加:

NSTimer *timer;

定時器變量。

在實現代碼中創建一個頁號變量:

#import "list_demoViewController.h" 

int pageNo=1;

在viewDidLoad方法中初始化定時器:

- (void)viewDidLoad { 
    timer=[NSTimer scheduledTimerWithTimeInterval:3 
                                           target:self 
                                         selector:@selector(getResults) 
                                         userInfo:nil 
                                          repeats:YES]; 
}

 

定時器間隔3秒,執行getResults函數:

-(void)getResults{ 
    if (pageNo>5) { 
        [timer invalidate]; 
        return; 
    } 
    resultsLabel.text=[[NSString alloc] initWithFormat:@"第%i頁",pageNo++]; 
}




為了模擬翻頁場景(為了測試內存是否泄露),需要用定時任務刷新界麵。寫了個小例子。

image

一上來是空白頁,3秒鍾後顯示第1頁,以後間隔3秒,頁號增加,直到第5頁。

首先,在controlller頭文件中增加:

NSTimer *timer;

定時器變量。

在實現代碼中創建一個頁號變量:

#import "list_demoViewController.h"

int pageNo=1;

在viewDidLoad方法中初始化定時器:

- (void)viewDidLoad { 
    timer=[NSTimer scheduledTimerWithTimeInterval:3 
                                           target:self 
                                         selector:@selector(getResults) 
                                         userInfo:nil 
                                          repeats:YES]; 
}

 

定時器間隔3秒,執行getResults函數:

-(void)getResults{ 
    if (pageNo>5) { 
        [timer invalidate]; 
        return; 
    } 
    resultsLabel.text=[[NSString alloc] initWithFormat:@"第%i頁",pageNo++]; 
}

在getResults函數中,當頁號大於5,定時器停止。

PDF    發送文章為PDF     

Tags: iosios threadios timeriphone

categories 計算機技術 | Marshal 2010年09月15日

這篇文章上的評論的 RSS feed TrackBack URI

Leave a Reply

 Name (required)

 Mail (will not be published) (required)

 Website


https://marshal.easymorse.com/archives/3265
  • 分類目錄

  • 功能

  • 標簽

  • 文章索引

     選擇月份   2012 年五月  (7)   2012 年四月  (4)   2012 年三月  (30)   2012 年二月  (12)   2012 年一月  (15)   2011 年十二月  (3)   2011 年十一月  (6)   2011 年十月  (4)   2011 年九月  (3)   2011 年七月  (29)   2011 年六月  (1)   2011 年五月  (5)   2011 年四月  (5)   2011 年三月  (12)   2011 年二月  (13)   2011 年一月  (32)   2010 年十二月  (24)   2010 年十一月  (14)   2010 年十月  (13)   2010 年九月  (29)   2010 年八月  (35)   2010 年七月  (19)   2010 年六月  (13)   2010 年五月  (3)   2010 年四月  (40)   2010 年三月  (35)   2010 年二月  (12)   2010 年一月  (33)   2009 年十二月  (36)   2009 年十一月  (41)   2009 年十月  (33)   2009 年九月  (14)   2009 年八月  (31)   2009 年七月  (17)   2009 年六月  (6)   2009 年五月  (20)   2009 年四月  (18)   2009 年三月  (15)   2009 年二月  (12)   2009 年一月  (16)   2008 年十二月  (20)   2008 年十一月  (27)   2008 年十月  (45)   2008 年九月  (20)   2008 年八月  (7)   2008 年七月  (15)   2008 年六月  (2)   2008 年五月  (3)   2008 年四月  (5)   2008 年三月  (4)   2008 年二月  (4)   2008 年一月  (3)   2007 年十二月  (4)   2007 年十一月  (2)   2007 年十月  (1)   2007 年九月  (2)   2007 年八月  (3)   2007 年七月  (2)   2007 年六月  (2)   2007 年五月  (2)   2007 年四月  (2)   2007 年一月  (1)   2006 年十二月  (3)   2006 年十一月  (2)   2006 年十月  (1)   2006 年八月  (1)   2006 年七月  (3)   2006 年六月  (3)   2006 年五月  (2)   2006 年四月  (1)   2006 年三月  (6)   2006 年二月  (3)   2005 年十二月  (1)   2005 年十一月  (1)   2005 年一月  (3)   2004 年十月  (1)   2004 年七月  (1)   2004 年六月  (1)   2004 年五月  (1)   2004 年四月  (1)   2004 年三月  (4)   2004 年二月  (2) 
  • Blogroll

最後更新:2017-04-04 07:03:38

  上一篇:go Core Data淺談係列之二 : 簡單的CURD
  下一篇:go Maven - Error Releasing Code to GitHub (Hangs After Push)