1

Hey I am writing a function that handles favorites within my app, as well as changing the image of the favorite button I want to change the image of a corresponding button in another UIScrollView DoubleScrollLeft.

The below code works HOWEVER I get the following eror when trying to favorite my first button with the tag '0' is there a reason for this? (the rest work).

-[UIScrollView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x14e3b0

Also once the images do change it pushes the title of the button off to the right, do i need to reset the frame etc.. when i change the background for state?

-(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 Answers 3

2

There seems to be a mis-connection where a UIScrollview is connected to the IBAction method. Check your IB connections carefully.

6
  • But why does it only happen when the sender tag is 0?
    – Wez
    Feb 13, 2012 at 22:00
  • The default tag value is 0. If the sender is not what you expect, that is another class, there is a connection issue.
    – zaph
    Feb 13, 2012 at 22:08
  • But it all works fine if I offset all of the sender tags by 1.
    – Wez
    Feb 14, 2012 at 10:42
  • Programming by coincidence is a bad idea. Take the time to understand every line of code written, that pays off in the end. There is no mystery, the computer does exactly what it is asked to do, the problem is we ask it to do the wrong thing to often.
    – zaph
    Feb 14, 2012 at 11:37
  • Very well said, which exactly why I am trying to find out why it does this when my tag is 0!?
    – Wez
    Feb 14, 2012 at 20:01
2

Your sender is somehow UIScrollview.

Change this line as, This should avoid the warning message.

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

[((UIButton *)sender) setImage:[UIImage imageNamed:@"favButton1.png"] forState:UIControlStateNormal];
1
  • Thanks but this dosen't work, I think the UIScroll view confusion is happening here > UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
    – Wez
    Feb 13, 2012 at 14:57
0

You have to cast the sender to a UIButton before you can call any of the UIButton methods on it...

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

etc.

[UIScrollView setBackgroundImage:forState:]

means that your sender is interpreted to be of type UIScrollView (not UIButton), and there is no method setBackgroundImage:forState: defined in UIScrollView.

1
  • Please can you show how I would use this along with [DoubleScrollLeft viewWithTag:i] to change the background image of a button within my second UIScrollView
    – Wez
    Feb 13, 2012 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.