【发布时间】:2015-02-05 03:31:39
【问题描述】:
我正在开发 Xcode 6 并尝试制作一个简单的倒计时应用程序。 我的应用程序真的很简单。 UI 有一个标签和一个按钮。当按钮被点击时,它应该从 10 开始倒计时。
这是我的代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSInteger count;
NSTimer *timer;
}
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)start {
count = 10;
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
};
-(void)timerFired:(NSTimer *)timer {
count -=1;
self.timerLabel.text = [NSString stringWithFormat:@"%i",count];
if (count == 0) {
[timer invalidate];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
程序编译得很好,但是当我单击按钮开始倒计时时没有任何反应。我哪里做错了?
编辑:非常感谢大家!问题解决了。我花了 3 个小时试图弄清楚我做错了什么,却发现这是一个愚蠢的错误。啊。 爱你stackoverflow!
【问题讨论】:
-
您的启动方法是否与 UIButton 的 UIControlEventTouchUpInside 事件相关联?
标签: ios objective-c iphone xcode nstimer