如何通过单击更改按钮上的图像并隐藏Objective C中的下一个图像?

时间:2012-11-02 06:06:13

标签: iphone objective-c ios xcode

我有两个按钮Btn1和Btn2,上面有图像Btn1image1.png,Btn2image1.png。 通过单击每个按钮,它必须分别显示Btn1image2.png和Btn2image2.png。

而且,如果单击Btn1,Btn2图像应该是相同的(Btn2image1.png)并且viz

如何实现这个?

我在这里添加我的作品,但是在Btn2点击

的情况下它不起作用
    -(IBAction)Bt1Click:(id)sender
    {

        if (Bt1.selected=YES)
        {
            UIImage *buttonImage = [UIImage imageNamed:@"Btn1image2.png"];
            [foodButt setBackgroundImage:buttonImage forState:UIControlStateSelected];

        }
        else
        {
            UIImage *buttonImage = [UIImage imageNamed:@"Btn2image1.png"];
            [foodButt setBackgroundImage:buttonImage forState:UIControlStateNormal];
        }


        [self.searchBaar resignFirstResponder];
    }

    -(IBAction)Btn2Click:(id)sender
    {
       if (Btn2.selected=YES)
       {
           UIImage *buttonImage = [UIImage imageNamed:@"Btn2image2.png"];
           [PeopButt setBackgroundImage:buttonImage forState:UIControlStateSelected];
       }
       else
       {
           UIImage *buttonImage = [UIImage imageNamed:@"Btn1image1.png"];
           [PeopButt setBackgroundImage:buttonImage forState:UIControlStateNormal];
       }

       [self.searchBaar resignFirstResponder];

    }

3 个答案:

答案 0 :(得分:0)

Try this for the first one and make the similar changes to second one:

-(IBAction)Bt1Click:(id)sender
    {

    if (Bt1.selected)
    {
        UIImage *buttonImage = [UIImage imageNamed:@"Btn1image2.png"];
        [foodButt setBackgroundImage:buttonImage forState:UIControlStateSelected];
        Bt1.selected = NO;

    }else{

        UIImage *buttonImage = [UIImage imageNamed:@"Btn2image1.png"];
        [foodButt setBackgroundImage:buttonImage forState:UIControlStateNormal];
        Bt1.selected = YES;
    }


    [self.searchBaar resignFirstResponder];

答案 1 :(得分:0)

您正在执行以下操作:

if (Bt1.selected = YES)

这不是你认为它做的那样。上面的代码将Bt1的selected属性设置为YES,您要执行以下操作:

if (Bt1.selected == YES)

=分配,==比较,这就是你想要的

答案 2 :(得分:0)

在这里尝试这个代码你也不要分别根据你的要求改变图像..我只考虑你的要求然后发布这个代码..

-(IBAction)Bt1Click:(id)sender
{

    if (Bt1.selected)
    {
        UIImage *buttonImage = [UIImage imageNamed:@"Btn1image2.png"];
        [foodButt setBackgroundImage:buttonImage forState:UIControlStateSelected];
        Bt1.selected = NO;

    }else{

        UIImage *buttonImage = [UIImage imageNamed:@"Btn1image1.png"];
        [foodButt setBackgroundImage:buttonImage forState:UIControlStateNormal];
        Bt1.selected = YES;
    }


    [self.searchBaar resignFirstResponder];
}
-(IBAction)Btn2Click:(id)sender
{

    if (Btn2.selected)
    {
        UIImage *buttonImage = [UIImage imageNamed:@"Btn2image2.png"];
        [PeopButt setBackgroundImage:buttonImage forState:UIControlStateSelected];
        Btn2.selected = NO;

    }else{

        UIImage *buttonImage = [UIImage imageNamed:@"Btn2image1.png"];
        [PeopButt setBackgroundImage:buttonImage forState:UIControlStateNormal];
        Btn2.selected = YES;
    }


    [self.searchBaar resignFirstResponder];
}