-1

I just want to change the text of a Label. There for I have a ViewController to a Xib-Interface where the label is created in and linked to the ViewController. I just create an instance of this view controller an want to change the text of the label:

NewFormScoreViewController * newPoints=[[NewFormScoreViewController alloc] init];
[newPoints.TheNewScoreLabel setText:[NSString stringWithFormat:@"%i",newScore]];
[newPoints.view setFrame:CGRectMake(500, 500, newPoints.view.frame.size.width,  newPoints.view.frame.size.height)];
[self.view addSubview:newPoints.view];

The view is visible but there is just the placeholder displayed, and not the new String. [NSString stringWithFormat:@"%i",newScore] returns the needed String, but it seems like the transfer doesn't work. But why? What's wrong?

BJS
  • 71
  • 3
  • 7
  • 2
    Do you want to change the value of yourLabel (resided in Second View Controller) from First View Controller ? – Bhavin Jul 18 '13 at 12:05
  • I want to change the label from the view, where the viewcontrollersview is loaded as subview, I tried also to add a method inside the NewFormScoreViewController which changes the label, the data was transfered correctly, but if I used [label setText:STRING] and after there were a NSLog that returns the Label text it was also (null) – BJS Jul 18 '13 at 12:40

5 Answers5

1

In a stringWithFormat: method, if your variable is an int, use %d, if it is a float, %f, %@ for NSString or other types.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
zbMax
  • 2,756
  • 3
  • 21
  • 42
  • BTW : does the init method of your class `NewFormScoreViewController` set a width and height? if not, when you set a new frame on the third line, you will obtain a frame (500 500 , 0 x 0) – zbMax Jul 18 '13 at 12:10
  • the view Controller is linked to a xib with the label and a backgroundimage and the subview added shows the bahckground image and the label with the placeholdertext – BJS Jul 18 '13 at 12:38
0

use This one...

[newPoints.TheNewScoreLabel setText:newScore];
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Dipak Narigara
  • 1,796
  • 16
  • 18
0

You didn't mention anything about "from where" you want to change the Value of your UILabel. If you simply want to change the value of UILabel from the same View Controller then you can do it like this way:

yourLabel.text = [NSString stringWithFormat:@"%d",newScore];

But if you want to change the value from other UIViewController then there are two possibilities. You can pass the Data Forward as well as Backward.

In that case, you should refer : Passing Data between View Controllers.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
0

You can use

[newPoints.TheNewScoreLabel setText:newScore];

OR

[newPoints.TheNewScoreLabel setText:[NSString stringWithFormat:@"%d",newScore]];

instead of

[newPoints.TheNewScoreLabel setText:[NSString stringWithFormat:@"%i",newScore]];

because

%d scans an integer as a signed decimal number, but %i allows defaults to decimal but also allows hexadecimal (if preceded by "0x") and octal if preceded by "0".

Jitendra
  • 5,055
  • 2
  • 22
  • 42
Akki Zaveri
  • 1
  • 1
  • 3
  • but the String [NSString stringWithFormat:@"%i",newScore] or what ever works fine – BJS Jul 18 '13 at 12:44
  • @BJS do you get "newScore" value what you need ? If yes then try [self.view addSubview:newPoints] only..tell me that works or not. – Akki Zaveri Jul 18 '13 at 20:07
0

Your code is ok.

But the class is a black box. Is this storyboard, nib file or manual implementation?

Is the pointer for the label object actally stored in the ivar?

Helge Becker
  • 3,219
  • 1
  • 20
  • 33