user3349668
user3349668

Reputation: 147

Changing the Second Button Image by Clicking the First Button IOS7

Hi I'm trying to change the button image by clicking the another button for example i have two buttons like button1 and button2 already this two button has images but now I'm trying to change the second button image by clicking the first button in second button the new image should replace the old image please tell me how to do it.

      - (IBAction)bt1:(id)sender {
           UIButton *btn = (UIButton*)sender;
           [btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
        }

  - (IBAction)bt2:(id)sender {

       }

   **[btn setBackgroundImage:[UIImage imageNamed:@"vv.png"]** forState:UIControlStateNormal];

This above code is used to change the button image but i want know how to change the second button image by clicking image.

Thanks

Upvotes: 0

Views: 206

Answers (3)

Raj
Raj

Reputation: 413

You might have the IBAction property of a second button, which you can access in your implementation (.m) file with self (e.g. self.secondButtonPropertyName), now you can write the code like below:

-(IBAction)bt1:(id)sender 
{               
   [self.btn2 setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
}

Upvotes: 2

Vishal Sharma
Vishal Sharma

Reputation: 1733

suppose you have two buttons btn1 and btn2

Now after them allocatng memory, you can use below Button selector,

btn1 selector,

-(IBAction)btn1selector:(id)sender
{
[btn2 setbackgroundimage: [uiimage imagenamed:@"your image"]];

}

you can do same for the other button selector.

Upvotes: 1

Sumit Mundra
Sumit Mundra

Reputation: 3901

first you set your both button tag..like btn1 tag =1 and btn2 tag =2

- (IBAction)bt1:(id)sender {
           UIButton *btn = (UIButton*)[self.view viewWithTag:2];
           [btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
        }

  - (IBAction)bt2:(id)sender {

       }

Upvotes: 1

Related Questions