簡體   English   中英

倒數刷新按鈕

[英]Countdown refresh button

我已經實現了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一起使用嗎?

您可以使用此方法

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

為此,您需要在.h文件中實現NSTimer聲明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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM