开发者

adding favorites feature to iPhone App iphone sdk

开发者 https://www.devze.com 2023-01-23 08:56 出处:网络
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 n

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];
}

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消