【发布时间】:2014-05-23 13:10:49
【问题描述】:
我想更改label.text,但是当我这样做并使用NSLog 进行检查时,它返回给我(null)。
这是我的代码:
NextViewController.h
@interface NextViewController (){
NSTimer *timerTen;
NSInteger countDown;
NSString *timer;
}
@end
@implementation NextViewController
static UILabel *lblTest;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)startCountDown{
countDown = 10;
timerTen = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dismissView) userInfo:nil repeats:YES];
}
-(void)dismissView{
timer = [NSString stringWithFormat:@"%ld", (long)countDown];
_lbl_countDown.text = timer;
NSLog(@"\nCountDown: %@\n", self.lbl_countDown.text);
countDown--;
if (countDown <= 0) {
SectionViewController *sectionView = [[SectionViewController alloc] init];
[sectionView.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
}
然后我有一个带有标签和更多内容的 XIB 文件,所以我在 .h 中使用了一个道具来将其从其他类中更改,但我无法更改它。
NextViewController.h
#import <UIKit/UIKit.h>
@interface NextViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *lbl_section;
@property (strong, nonatomic) IBOutlet UILabel *lbl_nextSection;
@property (strong, nonatomic) IBOutlet UILabel *lbl_countDown;
-(void)startCountDown;
@end
希望任何人都可以帮助我,因为我不知道这里发生了什么。
谢谢。
【问题讨论】:
-
你应该在主运行循环中添加定时器:[[NSRunLoop mainRunLoop] addTimer: timerTen forMode:NSRunLoopCommonModes];
-
你在哪里调用 startCountDown 方法?\
-
更新界面应该在主线程上完成,因此请确保在主线程上调用dismissView或调用performSelectorOnMainThread:...变体之一来设置文本。
标签: ios objective-c uiviewcontroller uilabel