我有一个标签,希望通过用户离开该标签来完成特定任务的秒数来更新。我决定使用每秒钟滴答作响的NSTimer,并调用函数updateTime(),这将减少包含剩余秒数的属性,然后更新标签。但是,标签未更新。我没有错误,也没有输出。我尝试使用updateTime()中的标签进行的任何更改均不起作用:mylabel.text,mylabel.frame,mylabel.font等。尽管如此,在其他任何地方,我都可以正常地修改标签。这是我的代码:
// ViewController.h
@interface ViewController : UIViewController
{
NSTimer* timer;
int secs;
UILabel* timerText;
}
@end
// ViewController.m
- (void)updateTime
{
secs--;
timerText = (UILabel*)[self.view viewWithTag:4]; // i have also tried putting this line in viewDidLoad. i also tried removing the property in the .h file and just doing UILabel* timerText = ....
NSString* updatedTime = [NSString stringWithFormat:@"%d", secs];
timerText.text = updatedTime;
}
- (IBAction)startButton_pressed:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
}
编辑:我现在收到“-[UIView setText:]:无法识别的选择器发送到实例”错误。NSLog(@"%@", timerText)
输出“>”,因此它不为null。在这种情况下,我得到“-[UIView setText:]:无法识别的选择器发送到实例0x9767b60”这是怎么回事?我该怎么办?我在一个新的项目上测试了类似的代码,并且该代码有效,不知道这里发生了什么。我的项目中可能有两个视图控制器可能导致这种情况?
谢谢大家的答复。我通过创建第二个ViewController类并将所有初始化都放在其viewDidLoad中来修复它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句