2011-09-25 138 views
1

我已經實現了風格UIBarSystemItem的刷新按鈕,每當按下該按鈕時它將被禁用10秒。倒計時刷新按鈕

我想通過顯示啓用之前剩下的時間來進一步改進它。什麼是最快捷的方法呢?

許多在此先感謝。

無論何時按下按鈕,都會調用。

-(void)refreshButton { 
[self performSelector:@selector(enableRefreshButton) withObject:nil afterDelay:10]; 
self.navigationItem.rightBarButtonItem.enabled = NO; 
} 

-(void)enableRefreshButton { 
self.navigationItem.rightBarButtonItem.enabled = YES; 
} 

編輯:刷新按鈕的風格UIBARSystemItemRefresh

我目前這樣做的答案表明我使用NSTimer。該按鈕確實啓用並相應地禁用,但文本不會更改。

@inteface ... { 
    int count; 
} 
-(void)viewDidLoad{ 
    UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshButtonPressed)];   
    self.navigationItem.rightBarButtonItem = refresh; 
} 
-(void)refreshButtonPressed { 
    //do something here... 
    count = 10; 
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimeLeft) userInfo:nil repeats:YES]; 
} 

-(void)updateTimeLeft{ 
    count --; 
    NSLog(@"%i", count); 
    self.navigationItem.rightBarButtonItem.title = [NSString stringWithFormat:@"%i", count]; 
    self.navigationItem.rightBarButtonItem.enabled = NO; 
    if (count == 0) { 
     self.navigationItem.rightBarButtonItem.enabled = YES; 
     [self.myTimer invalidate]; 
    } 
} 

我可以使用title屬性與UIBarButtonSystemItem?

+0

使用self.navigationItem.rightBarButtonItem.title屬性來更改欄按鈕的標題 – DShah

+0

經過我更新的答案... – DShah

回答

0

爲此,您需要實現的NSTimer 在.h文件中聲明int變量count。並根據您的代碼在viewDidLoad/viewWillAppear中用count==10進行初始化。

調用此方法performSelector

- (void)updateCount:(NSTimer *)aTimer { 
    count--; 
    self.navigationItem.rightBarButtonItem.enabled = YES; 
    self.navigationItem.rightBarButtonItem.title = [NSString stringWithFormat:@"%d",count]; 
    NSLog(@"%i", count); 
    if (count == 0) { 
     self.navigationItem.rightBarButtonItem.enabled = NO; 
     self.navigationItem.rightBarButtonItem.title = [NSString stringWithFormat:@"YOUR BAR BUTTON NAME"]; 
     [timer invalidate]; 
     timer = nil; 
    } 
} 

編輯

我覺得這個環節肯定會解決的問題。這跟你問的幾乎一樣。請通過鏈接。 http://www.richardhyland.com/diary/2008/12/21/some-cool-tips-and-tricks-for-the-iphone-sdk/

+0

能否請你提供一個關於我使用NSTimer更新的問題的建議? –

+0

那麼是什麼問題?它沒有運行? – DShah

0

您可以使用此方法

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats 
+0

我想我可以使用的NSTimer其中有一個返回一個時間間隔的目標之前離開屬性被解僱。但是,如何將按鈕標題設置爲每隔一秒的這個timeInterval呢? –

+0

據我所知,似乎沒有直接的方法可以做到這一點。或者,您可以手動維護秒數值(計數器的種類),並在每次調用目標方法(由scheduledTimerWithTimeInterval)時增加它,並在使計時器無效時終止計數器。 – sElanthiraiyan