簡體   English   中英

無法更改label.text,也不知道為什么

[英]Can't change label.text and don't know why

我想更改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中使用了prop來將其從其他類中更改,但是我無法更改它。

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

希望有人能幫助我,因為我不知道這里發生了什么。

謝謝。

在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方法中的[self startCountDown]調用函數

如果問題仍然存在:

  • 嘗試使用此方法: [_lbl_count setText@"myText"];

  • 在身份檢查器中,單擊標簽,取消選中“靜態文本”,然后選中“已啟用”“已啟用用戶交互”

  • 最后,在初始化后檢查變量timer是否不為null。

    timer = [NSString stringWithFormat:@"%ld", (long)countDown];

希望對您有所幫助

我編寫了以下代碼,以使用按鈕點擊從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];  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM