0

I wanted to add a string to a label and tried different things.

So this is what my code looks like:

@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);
}

That didn't work so I tried these too:

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

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

And I was so frustrated that I even tried this if that worked:

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

My console prints

2017-04-19 10:47:58.849359+0800 string = Intensity is at 100

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

So what am I doing wrong? Why the label.text stays null?

Community
  • 1
  • 1
F.Chen
  • 31
  • 8
  • Have you tried `[intensityLabel setText:@"hello"]` – Shamas S Apr 19 '17 at 03:06
  • Can you show where you print the label value to console? – Shamas S Apr 19 '17 at 03:07
  • Yes I tried this one too, forgot to add it because I deleted because it didn't worked. – F.Chen Apr 19 '17 at 03:09
  • I printed it at the last line : NSLog(@"label text = %@",intensityLabel.text); – F.Chen Apr 19 '17 at 03:10
  • Any of these ways should have worked. So the last thing I would say is that your `intensityLabel` is not correctly hooked up. Can you check if its value is `nil`? – Shamas S Apr 19 '17 at 03:12
  • The problem is simple - `intensityLabel` is `nil`. Do some debugging to see why. – rmaddy Apr 19 '17 at 03:13
  • Yes it is nil, how can I fix that? – F.Chen Apr 19 '17 at 03:18
  • Either your `intensityLevelView` is nil as well. Or it doesn't have any view with `tag:10` – Shamas S Apr 19 '17 at 03:20
  • Yeah... Both of them are nil, but I added a view with a tag. – F.Chen Apr 19 '17 at 03:23
  • 1
    If `intensityLevelView` is `nil` then you never connected your outlet. – rmaddy Apr 19 '17 at 03:46
  • did you check tag of intensityLabel or **is it subview of intensityLevelView** ? – KKRocks Apr 19 '17 at 05:28
  • So, I connected the intensityLevelView with my outlet (stupid error of mine). The intensityLabel isn't nil anymore but has the value of intensityLevelView. And I get the error in my console -[UIView setText:]: unrecognized selector send to instance. – F.Chen Apr 19 '17 at 06:48
  • So I found this http://stackoverflow.com/questions/11724003/why-uilabel-is-not-initialized and read the answer and the link and I didn't get it at all... – F.Chen Apr 19 '17 at 07:40

2 Answers2

0

Breakpoint on intensityLabel = (UILabel *) [intensityLevelView viewWithTag: 10]. The next line to see if intensityLabel is empty. Could be a tag is wrong.

Simon H
  • 2,495
  • 4
  • 30
  • 38
艾泽鑫
  • 1
  • 1
0

I found the solution to my problem...

I just needed to add one line to the -(void)viewdDidLoad {} and this is it:

[self.intensityLabel addSubview:intensityLevelView];
F.Chen
  • 31
  • 8