1

I want to know how to add the toggling feature to a UIButton, something like the user taps a unselected button the button becomes selected and stays selected till the user taps it again there by making unselected like it was before.

I was thinking of making an IBAction which changes it from unselected to selected, how can I do that?

Heres what I tried:

-(IBAction)toggle {

    //Toggle on implementation.

        button.selected = YES;
        button.highlighted = NO;
        button.enabled = YES;

    //Toggle off implementation.

    if (button.highlighted == YES) {

        button.selected = NO;
        button.highlighted = YES;
        button.enabled = NO;
    }
}

Problem...

-(IBAction)toggleFav {

    if (favButton == nil) {

        UIImage *unselectedImage = [UIImage imageNamed:@"favUntapped.png"];
        UIImage *selectedImage = [UIImage imageNamed:@"favTapped.png"];
        [favButton setImage:unselectedImage forState:UIControlStateNormal];
        [favButton setImage:selectedImage forState:UIControlStateSelected];
        [favButton setFrame:CGRectMake(0, 0, 40, 40)];
    }       

    if([favButton isSelected]){

        //Add to menu.
        [favButton setSelected:NO];
    } else {

        //Remove from menu.

        [favButton setSelected:YES];
    }
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Souljacker
  • 774
  • 1
  • 13
  • 35

4 Answers4

11
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImage *unselectedImage = [UIImage imageNamed:@"unselected.png"];
        UIImage *selectedImage = [UIImage imageNamed:@"selected.png"];
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        [b setBackgroundImage:unselectedImage forState:UIControlStateNormal];
        [b setBackgroundImage:selectedImage forState:UIControlStateSelected];
        [b addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [b setFrame:CGRectMake(0, 0, 40, 40)];
        [cell.contentView addSubview:b];
    }       
    return cell;
}

-(void) buttonPressed:(UIButton *)sender
{
    if([sender isSelected]){
        //...
        [sender setSelected:NO];
    } else {
        //...    
        [sender setSelected:YES];
    }

}

Your toggleFav code doesn't make much sense.
if (favButton == nil) { checks, if favButton is present. But if you are wiring it up with IB, it should always been there at that point. And if it wasn't how could the button call this method? So do it like this:

-(void)viewDidLoad
{
    //....
    UIImage *unselectedImage = [UIImage imageNamed:@"favUntapped.png"];
    UIImage *selectedImage = [UIImage imageNamed:@"favTapped.png"];
    [favButton setImage:unselectedImage forState:UIControlStateNormal];
    [favButton setImage:selectedImage forState:UIControlStateSelected];
    [favButton setFrame:CGRectMake(0, 0, 40, 40)];
    //....
}


-(IBAction)toggleFav:(UIButton *)sender {
    if([sender isSelected]){
        //...
        [sender setSelected:NO];
    } else {
        //...    
        [sender setSelected:YES];
    }
}

Here you'll find an example project, with a DetaiView, that holds a Button with the 2 states.
Note: I am saving the information of what button was selected in the NSUserDefaults. You should not do that. Instead you'll want to save it in the model. But as I dont have informations on your model, I am just using NSUserDefaults.

Zoe
  • 27,060
  • 21
  • 118
  • 148
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • as i wrote in ur [other question](http://stackoverflow.com/questions/4081154/adding-favorites-feature-to-iphone-app-iphone-sdk/4081265#4081265): 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 16:21
  • 2
    man, i have to say: ur attitude is really not helpful ("I don't want", "I don't have any code", "still waiting", "still waiting (again)"). There are a lot people around here, willingly to help u. But u should convince them, that u tried ur best to manage it on ur own. meaning: referring to docs, showing code, explaining exactly where u got stuck. – vikingosegundo Nov 26 '10 at 16:28
  • I tried the code but when I go out of the view where I implemented it. It doesn't remember the state. Also I implemented your code under a IBAction. – Souljacker Dec 04 '10 at 17:49
  • There was a small error: I was painting a new button at top of the old every time the cell was reloaded. see my fixed coded. – vikingosegundo Dec 04 '10 at 18:35
  • I only copied the -(void)buttonpressed. I did all of the rest in Interface Builder. – Souljacker Dec 04 '10 at 20:23
  • in `-tableView:cellForRowAtIndexPath:` i put the button generation into the `if (cell == nil)` block – vikingosegundo Dec 04 '10 at 20:25
  • I tried this out with a button. But it doesn´t save the state. – Souljacker Dec 04 '10 at 20:34
  • It should. You must do something wrong — probably something like I did. But without seeing ur code, i cannot even give a good guess. – vikingosegundo Dec 04 '10 at 20:36
  • I will add it to my question. – Souljacker Dec 04 '10 at 20:38
  • I will have a look on it tomorrow, as i need to go now. – vikingosegundo Dec 04 '10 at 20:41
  • I doesn´t work, but I should tell you that this view is a detailView. Which when a user taps a tableView cell is goes to this. – Souljacker Dec 05 '10 at 13:43
  • So when exactly does the button looses it state? – vikingosegundo Dec 05 '10 at 16:43
  • I don´t know exactly when it lost its state, but I copied the code and builded it and the button didn´t show up. – Souljacker Dec 06 '10 at 13:38
  • U should never just copy code, but understand it. in my `-viewDidLoad` i didnt add the button to the view. `[self.view addSubview:favButton]` – vikingosegundo Dec 06 '10 at 14:23
  • thanks for your instructions, but then does [self.view addSubview:favButton] add favButton to my view or does it create a new reference to my favButton? – Souljacker Dec 07 '10 at 14:11
  • http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html%23jumpTo_51 Seriously: Did you consider walking through some basic docs? this is simple but important stuff. here i posted some useful links: http://stackoverflow.com/questions/4190910/book-app-tutorial-iphone/4191037#4191037 – vikingosegundo Dec 07 '10 at 14:18
  • This is what I did. In IB I changed my buttons default and selected state to my unselected image and selected image. Then I implemented your code without the code under viewDidLoad. – Souljacker Dec 07 '10 at 14:26
  • Amd did u wire the button up? – vikingosegundo Dec 07 '10 at 14:29
  • yep, I wired my button to my favButton outlet. – Souljacker Dec 07 '10 at 16:13
  • how does ur property for favButton look like? – vikingosegundo Dec 07 '10 at 20:36
2
-(void)hitButton:(UIButton*)button
{
    buttonOnFlag = !buttonOnFlag;
    if( buttonFlag )
        [self performSelector:@selector(setHighlight:) withObject:button afterDelay:0];
}

- (void)setHighlight:(UIButton*)button 
{
    button.highlighted = true;
}
Steve Rogers
  • 1,963
  • 2
  • 17
  • 25
0

Use button.highlighted property

KingofBliss
  • 15,055
  • 6
  • 50
  • 72
0

You should code like this:

-(IBAction)toggle:(id)sender {

    //Toggle on implementation.
   if (sender.highlighted == NO)
     {
        sender.selected = YES;
        sender.highlighted = NO;
        sender.enabled = YES;
     }

    //Toggle off implementation.

   else{

        sender.selected = NO;
        sender.highlighted = YES;
        sender.enabled = NO;
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
KingofBliss
  • 15,055
  • 6
  • 50
  • 72