change image on click of button in objective c

前端 未结 3 450
粉色の甜心
粉色の甜心 2021-01-27 15:51

i draged 3 button in my .xib file (btn1,btn2,btn3 respectively) and initially i given default image to them, first.png

now when user clicks on btn1, image of btn1 should

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-27 16:22

    Set the images in your button call method like:

    BOOL first;
    -(void)firstBtnPressed
       {
       if(first == YES){
       [btn1 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
       [btn2 setImage:[UIImage imageNamed:@"second.png"] forState:UIControlStateNormal];
       first = NO;
       }
       else
       {
        first = YES;
        [btn1 setImage:[UIImage imageNamed:@"second.png"] forState:UIControlStateNormal];
        [btn2 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
       }
    }
    

提交回复
热议问题