我想更改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
希望有人能帮助我,因为我不知道这里发生了什么。
感谢。
答案 0 :(得分:0)
在ViewDidLoad或ViewWillAppear
中调用您的方法- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self startCountDown]
}
或强>
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startCountDown]
}
答案 1 :(得分:0)
尝试使用[self startCountDown]
方法
ViewDidLoad
首先调用该函数
如果问题仍然存在:
尝试使用此功能:[_lbl_count setText@"myText"];
在Identity Inspector中,点击标签取消选中&#39;静态文字&#39; ,然后选中&#39;已启用&#39; 和&#39;用户互动已启用&#39;
最后检查初始化后变量timer
是否为空。
timer = [NSString stringWithFormat:@"%ld", (long)countDown];
希望这将有所帮助
答案 2 :(得分:0)
我写了这段代码,用一个按钮来开始倒计时从10到segue:
在我的h:
@property (strong, nonatomic) IBOutlet UILabel *countdownCounter;
@property (strong, nonatomic) IBOutlet UIButton *countdownStarter;
在我的m:
static NSTimer *timer;
static int remainingCounts;
.
.
-(void)countDown
{
if (--remainingCounts == 0)
{
[timer invalidate];
timer = nil;
[self performSegueWithIdentifier:@"FromStartToBalancer" sender:self];
}
else
{
NSString * holdText = [NSString stringWithFormat:@"%d", remainingCounts];
[self.countdownCounter setText:holdText];
}
}
- (IBAction)goToBalancerPressed:(UIButton *)sender
{
self.countdownStarter.userInteractionEnabled = NO;
self.countdownStarter.alpha = .3;
remainingCounts = 10;
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countDown)
userInfo:nil
repeats:YES];
}