2

I want to add an automatic favorites feature that works like this:

I have a detail view with a cell name and a unchecked star. User taps on the unchecked star, star is checked and the specific cell name is added to another view. At anytime the user can go to the detailview and tap the star again and the star becomes unchecked and the cell name is deleted from the other view.

I want to do this with a custom button as the star and a tableview as the other view. Preferably using an IBAction or an IBOutlet.

My Code for my button in my detailView

-(IBAction)toggleFav:(UIButton *)sender {
if([sender isSelected]){

    //...
    [sender setSelected:NO];
    NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"valueSaver"] mutableCopy];
    [array removeObject:[NSString stringWithString:self.selectedSushi]];
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"valueSaver"];
    [array release];

} else {

    //...    
    [sender setSelected:YES];
    NSMutableArray *array = [[[NSUserDefaults standardUserDefaults] objectForKey:@"valueSaver"] mutableCopy];
    [array addObject:[NSString stringWithString:self.selectedSushi]];
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"valueSaver"];
    [array release];
}

}

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Souljacker
  • 774
  • 1
  • 13
  • 35
  • u can set a custom image for a UIButton. U will want to have 2 different images for selected and unselected. iboutlet is no problem – vikingosegundo Nov 05 '10 at 17:33

1 Answers1

4

Delegation is your friend:

  • Write a controller for that detail view, that holds the star.
  • This controller has a delegate member of the type id<AProtocolDefindeByYou> delegate.
  • In the Protocol-declaration you could have methods like informOfCheckTriggeredOnStar: and informOfUnCheckTriggeredOnStar:
  • You implement this delegegate-method in the controller of your favorites view and set this controller as delegate on each detail view.

Delegation & Protocols

edit
Quick'n'Dirty — I wrote a small sample code for you. It is very rough and not nice looking, but it demonstrate how it works.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • when i run the sample code for vscheck favorites it fails with 3 errors and 4 warnings showFavoritesTableViewController not found, and accessing unknown component tableView. – Souljacker Nov 03 '10 at 13:56
  • try again. I forgot to add some files – vikingosegundo Nov 03 '10 at 14:04
  • how do i make my favorite button highlighted so when the user taps on it again it goes off. – Souljacker Nov 05 '10 at 14:20
  • I really dont understand ur question. u better ask a new question with some description and code. – vikingosegundo Nov 05 '10 at 15:53
  • I've updated the code. I replaced the UISwitch with UIButtons with 2 different Star-images for selected/unselected. – vikingosegundo Nov 25 '10 at 21:06
  • the stars are not related to the tableview. they are part of a button. u can add a button to any view, that is (deriving from) a UIView. If u don't know this, u REALLY should read some documentation http://stackoverflow.com/questions/4190910/book-app-tutorial-iphone/4191037#4191037 – vikingosegundo Nov 26 '10 at 15:19
  • Listen: there is NOTHING special about this code. All used patterns are absolutely cocoa-standard. Any resource about cocoa(-touch) must deal with it. If u still have problems with understanding it, u need to start with the basics again. I think [iTunes U: Stanford — Developing Apps for iOS by Paul Hegarty](http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=395605774) is one of the best for beginners. [Here](http://stackoverflow.com/questions/4190910/book-app-tutorial-iphone/4191037#4191037) I collected more resources. – vikingosegundo Feb 01 '11 at 22:15
  • @vikingosegundo How do I change my value to be a name not a number? Here is what I mean [array removeObject:[NSNumber numberWithInt:self.selectedSushi]]; (and the same for the addObject). I want to change the numberWithInt and NSNumber so it detects the name of my cell. Can I use NSString or what? – Souljacker Feb 15 '11 at 10:07
  • did u even consider working through the docs I gave u the links to? – vikingosegundo Feb 15 '11 at 10:19
  • i did and I figured it out but I cant get it to save the favButtons state. – Souljacker Mar 07 '11 at 15:14
  • that is a question for its own. But one solution could be using `NSUserDefaults` – vikingosegundo Mar 07 '11 at 15:47
  • i used NSUserDefaults look at my question, I am going to add my code there. – Souljacker Mar 07 '11 at 18:56
  • You should really ask that as a new question. There are many things to consider. I assume u r doing a Sushi application. If Sushi is an Entity in CoreData or SQLite, the information, if it is selected as favorite should be stored there. This question here doesn't cover it. and as this is an old thread, not many people will see it. Especially nobody will read all the comments. Just asked a new question and give informations about the models of ur application. – vikingosegundo Mar 08 '11 at 12:44
  • Hi Viki, I have tried your sample code and it works great! I have noticed that if I killed the application and restart again, it clears up NSUserDefaults. Is it possible to keep selection persisted? I know I can use SQLite but just wondering if it is possible with NSUserDefaults. Thanks. – Paresh Masani Jun 29 '11 at 16:04
  • 1
    you could save things like cells indexPathes in an array and that in the NSUserDefaults. once u create the cells you check, if the indexPath is inside the selected list. Don't forget to upvote. – vikingosegundo Jun 29 '11 at 16:10
  • Sorry Viki I didn't follow you. Do you mean Array won't get cleared when application killed? Can you elaborate it with some tiny code please. Other way I learnt is storing information in Plist file. – Paresh Masani Jun 29 '11 at 16:35
  • Fill an array with all indexPathes of selected cells and store it in nsuserdefaults. That is quite easy, but I don't have time to write that code. Search for nsuserdefaults. There ate enough codes here in SO – vikingosegundo Jun 29 '11 at 18:07
  • Hi viki, storing value on NSMutableDictionary to NSUserDefaults doesn't seem to be working! I have created and edited new [question](http://stackoverflow.com/questions/6585200/iphone-preserve-nsuserdefaults-values-when-application-is-killed) Also given the code snippet in Edit: section. Could you please give your thoughts there. Thanks. – Paresh Masani Jul 06 '11 at 11:48
  • +1 for creating a whole sample project for this guy. Excellent. – mszaro Feb 21 '14 at 00:50