倒计时刷新按钮

发布于 2024-12-06 04:09:37 字数 1458 浏览 1 评论 0原文

我已经实现了 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 一起使用吗?

I have implemented a refresh button of style UIBarSystemItem which will be disabled for 10s whenever it is pressed.

I want to further improve it by showing the time left before it is enabled. What is the quickest way to do it?

Many thanks in advance.

Called whenever the button is pressed.

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

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

EDIT: The refresh button is of style UIBARSystemItemRefresh.

I am currently doing this as the answer suggests I use NSTimer. The button does get enabled and disable accordingly but the text doesn't change.

@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];
    }
}

Can I use the title property with UIBarButtonSystemItem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在风中等你 2024-12-13 04:09:37

为此你需要实现 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/

For this you need to implement NSTimer
declare int variable count in .h file. and initialize it with count==10 in viewDidLoad/viewWillAppear as your code goes.

call this method in 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;
    } 
} 

EDIT

I think this link would definitely solved problem. This is almost same as you were asking. Please go through the link. http://www.richardhyland.com/diary/2008/12/21/some-cool-tips-and-tricks-for-the-iphone-sdk/

小忆控 2024-12-13 04:09:37

你可以使用这个方法

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

You could use this method

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文