倒计时刷新按钮
我已经实现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此你需要实现 NSTimer
在 .h 文件中声明
int
变量count
。并在代码运行时在viewDidLoad/viewWillAppear
中使用count==10
对其进行初始化。在performSelector中调用这个方法
编辑
我认为这个链接肯定会解决问题。这和你问的几乎一样。请浏览链接。 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
variablecount
in .h file. and initialize it withcount==10
inviewDidLoad/viewWillAppear
as your code goes.call this method in performSelector
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/
你可以使用这个方法
You could use this method