发送到实例的无法识别的选择器更改UIButton背景时

时间:2012-02-13 14:41:49

标签: ios uiscrollview tags uibutton instance

嘿我正在编写一个处理我的应用程序中的收藏夹的函数,以及更改收藏夹按钮的图像我想要更改另一个UIScrollView DoubleScrollLeft中相应按钮的图像。

下面的代码有效 HOWEVER 当我尝试用标签'0'收藏我的第一个按钮时出现以下错误是否有原因? (其余的工作)。

- [UIScrollView setBackgroundImage:forState:]:无法识别的选择器发送到实例0x14e3b0

此外,一旦图像发生变化,它会将按钮的标题推向右侧,我是否需要重置帧等...当我更改背景状态时?

-(void)favButtons:(id)sender {

int i = [sender tag];
NSString *fav = [NSString stringWithFormat:@"%i", i];

    if ([[Favinsults objectForKey:fav] isEqualToString:@"0"]){

    [sender setImage:[UIImage imageNamed:@"favButton1.png"] forState:UIControlStateNormal];

    UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonD1.png"] forState:UIControlStateNormal];

    } else {

    [sender setImage:[UIImage imageNamed:@"favButton0.png"] forState:UIControlStateNormal];

    UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonD0.png"] forState:UIControlStateNormal];

    }
}

3 个答案:

答案 0 :(得分:2)

您的发件人是UIScrollview。

将此行更改为,这应该避免警告消息。

[sender setImage:[UIImage imageNamed:@"favButton1.png"] forState:UIControlStateNormal];

[((UIButton *)sender) setImage:[UIImage imageNamed:@"favButton1.png"] forState:UIControlStateNormal];

答案 1 :(得分:2)

似乎是UIScrollviewIBAction方法相关联的错误连接。仔细检查您的IB连接。

答案 2 :(得分:0)

您必须先将发件人转发给UIButton,然后才能调用它上面的任何UIButton方法...

UIButton * newbutton = (UIButton*)sender;
[newbutton setImage:[UIImage imageNamed:@"favButton1.png"] forState:UIControlStateNormal];

[UIScrollView setBackgroundImage:forState:]

表示您的发件人被解释为UIScrollView类型(不是UIButton),并且UIScrollView中没有定义方法setBackgroundImage:forState: