tags:

views:

36

answers:

1

This is on a sidenote to my previous question about ratingview

I have this code:

[starView displayRating:1.5];

...however, I want it to be changed according to this:

-(void)ratingChanged:(float)newRating { 
    ratingLabel.text = [NSString stringWithFormat:@"Rating is: %1.1f", newRating];
    //....

How can I make it displayRating: newString...

Can anyone show me how to do this please?

Update

Update i have this code in place from what ive seen from your instructions (without the labels those are irrevelant)

 -(void)viewDidLoad {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];


NSLog(@"Initializing rating view");
[starView setImagesDeselected:@"0.png" partlySelected:@"1.png" fullSelected:@"2.png" andDelegate:self];
[starView displayRating:[[defaults floatForKey:@"Rating"] stringValue]]; }

however i am getting an error message incompatible type for argument 1 of display rating and cannot convert to pointer type errors while building...

A: 

If you want to save a simple one value rating between application launches you should use NSUserDefaults like this:

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
// Save
[defaults setFloat:newRating forKey:@"Rating"];
// Read
float *savedRating=[defaults floatForKey:@"Rating"];

Update:

im sorry im a 16 year old and i have no idea how to implement this into my code. ive never used the nsuserdefault class before i have this code how would i do it.

-(void)viewDidLoad { 
    [super viewDidLoad]
    theLabel.text = myLabel
    theLabel2.text =myLabel2
    NSLog(@"Initializing rating view")
    [starView setImagesDeselected:@"0.png" partlySelected:@"1.png" fullSelected:@"2.png" andDelegate:self]
    [starView displayRating:savedRating]
}

Well, to start, you would most likely want to put this code in viewDidAppear instead so that it is called every time the view appears. viewDidLoad is only called the first time the view controller loads from the nib file.

Suppose you wanted to set the text of theLabel to the value of the saved rating. It would look like this:

-(void)viewDidLoad { 
    [super viewDidLoad]
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    theLabel.text = [[defaults floatForKey:@"Rating"] stringValue];
    theLabel2.text =myLabel2
    NSLog(@"Initializing rating view")
    [starView setImagesDeselected:@"0.png" partlySelected:@"1.png" fullSelected:@"2.png" andDelegate:self]
    [starView displayRating:savedRating]
}
TechZen
where would i implement this code in the viewcontroller or in the rating.h or rating.m?
Alex
Since user defaults is a universal singleton, you can call it anywhere you need it. Usually, you call it right where the data will be used, in this case, a view controller so the data can be immediately displayed in a label in a view the controller manages.
TechZen
im sorry im a 16 year old and i have no idea how to implement this into my code.ive never used the nsuserdefault class beforei have this code how would i do it-(void)viewDidLoad { [super viewDidLoad]; theLabel.text = myLabel; theLabel2.text =myLabel2; NSLog(@"Initializing rating view"); [starView setImagesDeselected:@"0.png" partlySelected:@"1.png" fullSelected:@"2.png" andDelegate:self]; [starView displayRating:savedRating]; }
Alex
i tried your code however it is not working im getting xcode build erros saying its missing varius stuff like ; and that somethings are undeclared. in ur code im seeing some mistakes 1) is that im not trying to get the label.text to show the key for rating that part should be left out im trying the nsuserdefault for the display rating. where i have saved rating there was a static number.... thank you and if u can please check if ur code works to make sure im not doing something wrong thanks
Alex
The code I posted should be taken as an example. It's not complete. It's just to show an example of how to retrieve a float value saved in user defaults and then insert that value as string into a label. You have to remember that I don't know what your program looks like or even what it is supposed to do. I can't write code that will work in your program based on the limited information I have. You'll have to adapt this example to the specifics of your program.
TechZen