无法更改 label.text 并且不知道为啥
Posted
技术标签:
【中文标题】无法更改 label.text 并且不知道为啥【英文标题】:Can't change label.text and don't know why无法更改 label.text 并且不知道为什么 【发布时间】: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:...变体之一来设置文本。 【参考方案1】:在 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]
【讨论】:
诀窍是我从其他类调用这个函数,我不想在 viewDidLoad 中调用它。 你到底想要什么,你想在什么时候设置标签? 我想从另一个视图更改 UILabel,就这个,我只想更改标签文本而不调用[self presentVieController:View animated:YES completion:nil];
view.yourLabel.text = @"在此处设置文本";在 [self presentVieController:View Animation:YES completion:nil] 之前添加;【参考方案2】:
尝试先在ViewDidLoad
方法中使用[self startCountDown]
调用函数
如果问题仍然存在:
尝试使用这个:[_lbl_count setText@"myText"];
在 Identity Inspector 中,点击标签取消选中 'Static Text' 并选中 'Enabled' 和 'User Interaction Enabled'
最后检查你的变量timer
在你初始化后不为空。
timer = [NSString stringWithFormat:@"%ld", (long)countDown];
希望对你有帮助
【讨论】:
变量timer
不为空,这就是为什么我不知道为什么我不能设置标签。而且我使用了[_lbl_count setText@"myText"];
并且无法正常工作,但是谢谢:S
你为什么不用timer = [NSString stringWithFormat:@"%d", countDown];
这是我用过的,计时器是一个整数,但是当我在标签上使用它时,它的 null :S【参考方案3】:
我编写这段代码是为了使用一个按钮点击来开始从 10 到 segue 的倒计时:
在我的 h:
@property (strong, nonatomic) IBOutlet UILabel *countdownCounter;
@property (strong, nonatomic) IBOutlet UIButton *countdownStarter;
在我的米:
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];
【讨论】:
以上是关于无法更改 label.text 并且不知道为啥的主要内容,如果未能解决你的问题,请参考以下文章