0

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

此外,如果单击 Btn1,则 Btn2 图像应该相同(Btn2image1.png)并且即

如何做到这一点?

我在这里添加我的工作,但在 Btn2 click 的情况下它不起作用

    -(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];

    }
4

3 回答 3

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];
于 2012-11-02T06:14:46.237 回答
0

您正在执行以下操作:

if (Bt1.selected = YES)

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

if (Bt1.selected == YES)

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

于 2012-11-02T06:15:32.870 回答
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];
}
于 2012-11-02T06:23:15.290 回答