UILabel.text不会添加NSString并保持为空

时间:2017-04-19 03:03:06

标签: ios objective-c uilabel

我想在标签上添加一个字符串并尝试不同的东西。

所以这就是我的代码:

@interface DeviceControl : UIViewController

@property (weak, nonatomic) IBOutlet UIView *intensityLevelView;
@property (weak, nonatomic) UILabel *intensityLabel;

- (IBAction)intensitySliderDragged:(id)sender

@end

@synthesize intensityLabel;
@synthesize intensityLevelView;

- (IBAction)intensitySliderDragged:(id)sender {

intensityLevel = intensitySlider.value;

int newIntensityLevel = (int)intensityLevel; //because intensityLevel is a float and I needed an integer.
intensityLabel = (UILabel*)[intensityLevelView viewWithTag:10];
NSString *string = [NSString stringWithFormat:@"Intensity is at %d",newIntensityLevel];
intensityLabel.text = string;
NSLog(@"string = %@", string);
NSLog(@"label text = %@",intensityLabel.text);
}

这不起作用,所以我也尝试了这些:

intensityLabel.text = [intensityLabel.text stringByAppendingFormat:@"%d",newIntensityLevel];

intensityLabel.text = [NSString stringWithFormat:@"Intensity is at %d", newIntensityLevel];

我非常沮丧,如果可行,我甚至尝试了这个:

intensityLabel.text = @"damn"; //it didn't worked...

我的控制台打印

  

2017-04-19 10:47:58.849359 + 0800 string =强度为100

     

2017-04-19 10:47:58.849484 + 0800 label text =(null)

那么我做错了什么?为什么label.text保持为空?

2 个答案:

答案 0 :(得分:0)

intensityLabel = (UILabel *) [intensityLevelView viewWithTag: 10]上的断点。查看intensityLabel是否为空的下一行。可能是标签错了。

答案 1 :(得分:0)

我找到了解决问题的方法......

我只需要在 - (void)viewdDidLoad {}中添加一行,就是这样:

            mpu9250_read_accel(&AccValue);
            AX =    ((AccValue.x *9800)/(16384));
            AY =    ((AccValue.y *9800)/(16384));
            AZ =    ((AccValue.z *9800)/(16384));