1

I have two buttons on my first view,so when I click on one of the button the view changes,so I have two images one for the default state and one for the selected state, first i tried with xib,goin into the properties and changing the states and then selecting the proper images and when I build and run my code,On cliking the image doesnt change..

So I did this way through code

- (IBAction) handleButton:(id)sender
{
    UIButton *button = (UIButton*)sender;
    int tag = [button tag];

    switch (tag)
    {
        case BUTTON_1:
            if ([m_Button1 isSelected]) 
            {

 [m_Button2 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];                                                                                 
                [m_Button1 setSelected:NO];
            }
            else 
            {
              [m_Button1 setImage:[UIImage imageNamed:@"image_pressed.png"] forState:UIControlStateSelected];   
                [m_Button1 setSelected:YES];
            }
            [self displaymethod1];
            break;
        case BUTTON_2:
            [self displaymethod2];
            break;
        default:
            break;
    }
}

here the image changes when i click on it and i go to diffrent view..when i again come back to my first view,the button is still in selected mode..so How shall i fix this..

Waiting for your reply

Rais Alam
  • 6,970
  • 12
  • 53
  • 84
Ranjit
  • 4,576
  • 11
  • 62
  • 121

2 Answers2

16

I think is a bit simpler through IB.

When you add a regular Round Rect Button in IB you can modify its behavior by going to the Button section in the Attributes Inspector panel.

First select the images you want for it's default state by leaving the State Config in Default. There is an image and a background image properties for this. Once that is set, you can change the State Config to Highlighted and select the image you want to show when the button is highlighted.

NOTE: This is for .

rjgonzo
  • 1,761
  • 1
  • 16
  • 29
  • hi rjgonzo,I did that but it the image wont change when i click on it – Ranjit Sep 08 '11 at 05:46
  • 2
    Make sure the second `State Config` was `Highlighted` and not `Selected`. – rjgonzo Sep 08 '11 at 12:43
  • thanks a lot..it works like a charm...and saves a lot of coding..thanks a ton – Ranjit Sep 08 '11 at 13:32
  • hey i have one more issue..I have two functions,which make a NSURL connection..So should I should i handle delegate functions in this case.. – Ranjit Sep 08 '11 at 13:44
  • I think is better if you post this as a formal question if there isn't one yet. I haven't work with NSURL so I am not sure if I can answer your question and is not clear to me what the issue is. Good luck. – rjgonzo Sep 08 '11 at 13:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3273/discussion-between-rjgonzo-and-ranjit) – rjgonzo Sep 08 '11 at 13:49
0

when your view will appear then you have to initiate all the variable and UI property in view will appear method. (not necessary in all cases solution is only for your requirement)

There you can set your button default state and image.

-(void)viewWillAppear:(BOOL)animated
{
 [m_Button2 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];                                                           
[m_Button1 setSelected:NO];
...
}
utkal patel
  • 1,321
  • 1
  • 15
  • 24