iOS Storing Star Ratings.

Ernie11

macrumors member
Original poster
Apr 4, 2014
37
0
[UPDATED POST]
Im going to be posting what I have now after changing star rating controllers. For the storing part I got this:
Code:
    if (Appetizer) {
        [Appetizer setValue:dishName.text forKey:@"name"];
        [Appetizer setValue:restaurantName.text forKey:@"restaurant"];
        [Appetizer setValue:statusLabel.text forKey:@"rating"];
        [Appetizer setValue:notes.text forKey:@"notes"];
    } else {
        NSManagedObject *newAppetizer = [NSEntityDescription insertNewObjectForEntityForName:@"Appetizers" inManagedObjectContext:context];
        [newAppetizer setValue:dishName.text forKey:@"name"];
        [newAppetizer setValue:restaurantName.text forKey:@"restaurant"];
        [newAppetizer setValue:statusLabel.text forKey:@"rating"];
        [newAppetizer setValue:notes.text forKey:@"notes"];
For what I know its storing the variable thats on statusLabel. Couldnt figure out another way to store a number. On the viewcontroller that I need the stars on I have to put this on the viewdidload:
Code:
    self.rateView.notSelectedImage = [UIImage imageNamed:@"kermit_empty.png"];
    self.rateView.halfSelectedImage = [UIImage imageNamed:@"kermit_half.png"];
    self.rateView.fullSelectedImage = [UIImage imageNamed:@"kermit_full.png"];
    self.rateView.rating = 0;
    self.rateView.editable = YES;
    self.rateView.maxRating = 5;
    self.rateView.delegate = self;
On this viewconroller I just want to display the stored stars and I noticed that if I change the 0 in self.rateView.rating = 0; to another number (max 5) it will show that many stars. So im looking to make self.rateView.rating = @"rating" but idk how to write that out. Ive tried various ways and cant get it to accept it without errors.


[OLD POST]Hello everyone. Im a bit new to programming and ive been stuck for a couple of days on this problem. Im trying to add a 5 star rating on an app im making. I get it to display just like I want it, but I cant figure out to save my life how to save the rating into the attribute. I currently have 3 attributes other than rating and I get those to store perfectly. I need to store them and then retrieve them on another viewcontroller. I am using mmStarRating controller from here: https://www.iphonelife.com/blog/31369/unleash-your-inner-app-developer-adding-navigation. I will post parts of my script to help out with the brainstorming.

Where I store the info into the attributes (they come from textfields):
Code:
- (IBAction)saveButton:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];
    if (Appetizer) {
        [Appetizer setValue:dishName.text forKey:@"name"];
        [Appetizer setValue:restaurantName.text forKey:@"restaurant"];
        [Appetizer setValue:notes.text forKey:@"notes"];
    } else {
        NSManagedObject *newAppetizer = [NSEntityDescription insertNewObjectForEntityForName:@"Appetizers" inManagedObjectContext:context];
        [newAppetizer setValue:dishName.text forKey:@"name"];
        [newAppetizer setValue:restaurantName.text forKey:@"restaurant"];
        [newAppetizer setValue:notes.text forKey:@"notes"];
    }
}
How I retrieve the information and display it:
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (Appetizer) {
        [dishName setText:[Appetizer valueForKey:@"name"]];
        [restaurantName setText:[Appetizer valueForKey:@"restaurant"]];
        [notes setText:[Appetizer valueForKey:@"notes"]];
    }
}
Also I cant figure out what I would change setValue and setText to considering its not a textfield and not a string for what I understand. Thanks in advance.

E.
 
Last edited:

Ernie11

macrumors member
Original poster
Apr 4, 2014
37
0
I have been working on it more. I changed out from mmStarRating to this tutorial: http://www.raywenderlich.com/1768/uiview-tutorial-for-ios-how-to-make-a-custom-uiview-in-ios-5-a-5-star-rating-view. But once again im stuck. I think I have gotten a value to store in the attribute but im not sure since I cant recall it. Under this tutorial I set that the statusLabel save as a string to the attribute "rating" with: [statusLabel setText:[Appetizer valueForKey:mad:"rating"]];. During this tutorial I have to put a line that says: self.rateView.rating = 0; in the viewcontroller im using. I using this viewcontroller to recall the info. But how can I get the 0 to display what saved on @"rating"? Rating is an attribute from the entity Appetizers if that matters. I think this is the last step I need to get this working. All the Labels will then just be hidden since I dont need them to display.

E.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,565
3,978
Instead of this:
self.rateView.rating = @"rating"
I think this might work:
Code:
self.rateView.rating = [appetizer valueForKey:@"rating"];
This may not work because I'm not quite sure what valueForKey will return... I think it may be an NSNumber. I suggest learning a bit about debugging and introspection to find out what that method returns, if it doesn't return what you want / need.
 

Ernie11

macrumors member
Original poster
Apr 4, 2014
37
0
I had tried that. The error it gives me is: "Assigning to 'float' from incompatible float 'id'." I have the attribute set to string, if I change it to float I still get this error. Im not sure if this is what I have to do but if the string was changed to float somehow and then float = float?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,565
3,978
I had tried that. The error it gives me is: "Assigning to 'float' from incompatible float 'id'." I have the attribute set to string, if I change it to float I still get this error. Im not sure if this is what I have to do but if the string was changed to float somehow and then float = float?
I think the return value will be an NSNumber, meaning you need to call floatValue on it before you can use it. Not certain as I generally avoid Core Data.
 

Ernie11

macrumors member
Original poster
Apr 4, 2014
37
0
Ok I looked up floatValue so I could get information on it and this is what I have made. This has no errors according to xcode.
Code:
NSString *currentRating = [Appetizer valueForKey:@"rating"];
    float r = [currentRating floatValue];
And I put r:
Code:
self.rateView.rating = r;
It doesnt display the marked stars though. Yet my statusLabel on viewDidLoad does show the correct number.
Code:
    [super viewDidLoad];
    if (Appetizer) {
        [dishName setText:[Appetizer valueForKey:@"name"]];
        [restaurantName setText:[Appetizer valueForKey:@"restaurant"]];
        [statusLabel setText:[Appetizer valueForKey:@"rating"]];
        [notes setText:[Appetizer valueForKey:@"notes"]];
Btw thanks for helping me.
 

Ernie11

macrumors member
Original poster
Apr 4, 2014
37
0
Ok I got it all working. With your suggestions I researched and played around a little and got it working. Thanks for the help.

E.