Unrecognised selector sent to instance When changing UIButton background - ios

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

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

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

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.

Related

Set images (from array) for buttons by their tag

I have an array of images in viewDidLoad method as follow :
images = [NSArray arrayWithObjects:#"make_fun.png",
#"smile-2.png",
#"cream_happy_ice.png",
#"big_smile.png",
#"fun.png",
#"happy_santaclaus",
#"make_fun.png",
#"smile-2.png",
#"cream_happy_ice.png",
#"big_smile.png",
#"fun.png",
#"happy_santaclaus",nil];
This array contains name of images.
I have also 12 buttons on storyboard and I tagged all buttons from 0 to 11.
What i want to implement is very simple - when the user clicks on the one of the buttons its tag is used as index in images array to set the background of the button. This is my code :
- (IBAction)onClickButton:(id)sender {
UIButton *button = (UIButton*)sender;
[button setImage:[UIImage imageNamed:[images objectAtIndex:[sender tag]] forState:UIControlStateNormal];
}
But I get this error in the editor:
Thread 1 : EXC_BAD_ACCESS (Code = 1, address = 0x1)
I have a look in many questions on stackoverflow by i can't find the solution.
Thanks!!!
Try this way, use [button tag] instead [sender tag]
- (IBAction)onClickButton:(id)sender {
UIButton *button = (UIButton*)sender;
[button setImage:[UIImage imageNamed:images[button tag]]
forState:UIControlStateNormal];
}
Try this. Use tag on button instead of sender. It may help you.
- (IBAction)onClickButton:(id)sender {
UIButton *button = (UIButton*)sender;
[button setImage:[UIImage imageNamed:[images objectAtIndex:[button tag]] forState:UIControlStateNormal];
}
The problem is that the generic type id of sender doesn't know a property tag.
An easy solution is to use UIButton as parameter type
- (IBAction)onClickButton:(UIButton *)button {
button.image = [UIImage imageNamed:images[button.tag] forState:UIControlStateNormal];
}
and dot notation avoids a bunch of brackets.
First make sure the UIImage instance in not nil
UIImage *image = [UIImage imageNamed:[images objectAtIndex:[button tag]];
if (image) {
[button setImage:[UIImage imageNamed:[images objectAtIndex:[sender tag]] forState:UIControlStateNormal];
} else {
// something is wrong with the image you are trying to assign.
}
if the image is indeed there, then try changing the button type from System to Custom in the interface builder

How to change button's Image on clicking it in ios

Confession: I have been through every possible link I could find, but unfortunately none work for me.
I am creating buttons dynamically. When a button is selected, it should change the color of the selected button and the rest should remain the default color. This way, the user can identify which button is selected.
Suppose there are 3 buttons: all blue in color, and when I select the first one, it should change to white and other two should remain blue. When I select the second, the first one should go back to blue and the second should now be white.
btnCounts data I am fetching from server and would vary.
-(void)createButtons{
for (int i=0; i<btnCounts; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTag:i];
[btn setFrame:CGRectMake(xVal, 0, width/btnCounts, 40)];
// [btn setImage:[UIImage imageNamed:#"btn_image.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"white.png"] forState:UIControlStateSelected];
[btn setTitle:name forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrV addSubview:btn];
xVal += self.view.frame.size.width/btnCounts+1;
}
}
Now, in btnClicked: method I am passing the button as a parameter, so that I can use selected button.
-(void)btnClicked:(UIButton *)button
{
int tag = (int)button.tag;
//...
}
Please help me to find what I am missing here.
Thanks.
Create an instance UIButton variable to house the selected button so that you can deselect it when the next button is tapped. If you only pass the currently-tapped button to your btnClicked: method, you won't know which button to deselect (if any).
#implementation ClassName {
UIButton *previousButton;
}
...
- (void)btnClicked:(UIButton *)button
{
if(previousButton)
[previousButton setBackgroundColor:[UIColor blueColor]];
previousButton = button;
[button setBackgroundColor:[UIColor whiteColor]];
// do whatever else you need to do here
}
For that you need to store selected button in temporary object.
Create button in interface(.h) file:
UIButton *selectedButton;
Than add following line in -(void)createButtons method before for loop.
selectedButton = nil;
for() {...}
Here selectedButton is set to nil because initially none of the buttons are selected.
And finally replace your -(void)btnClicked:(UIButton *)button method with following method.
-(void)btnClicked:(UIButton *)button {
if(selectedButton) {
selectedButton.selected = NO;
}
button.selected = YES;
selectedButton = button;
}
If you want to make any button initially selected than assign that button to selectedButton like in btnClicked method.

How to remove UIButton in iOS7

I want to create and remove uibutton. (iOS7)
My viewDidLoad function creates uibutton in the following way:
for (char a = 'A'; a <= 'Z'; a++)
{
position = position+10;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(getByCharacter:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%c", a] forState:UIControlStateNormal];
button.frame = CGRectMake(position, self.view.frame.size.height-40, 10.0, 10.0);
button.tintColor = [UIColor whiteColor];
[self.view addSubview:button];
}
Now I want to remove specific uibutton based on button text.
- (IBAction)getByCharacter:(id)sender{
}
I don't know how to do this. Can anyone provide some sample code?
You can remove the button by calling
[sender removeFromSuperview];
in your callback (getByCharacter:). When you attach the selector to the button, the sender parameter will be the button which triggered the event. That means that you have an instance of it. With this instance you can now calll the removeFromSuperview method.
First you need to get the button's title text and then remove it from superView if it matches to the specific character,
- (IBAction)getByCharacter:(id)sender{
UIButton *myButton = (UIButton *) sender;
if([myButton.titleLabel.text isEqualToString:#"C"]{
[myButton removeFromSuperView];
}
}

Button is supposed to change image after click, but (randomly) changes images of other buttons too

In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.

UIButton - On touch change image

When I touch the button at that time I want to change image & when i release the touch button image is as it is.
I want to apply below code but it's not with my expectation.
please give me any suggestion.....
-(IBAction)actionEnter:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateSelected];
[sender setSelected:YES];
}
You can use UIControlStateHighlighted for this.
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateHighlighted];
You can also set this from interface builder by setting the image for highlighted state.
I think this should do it. Set the images after creating the button
[yourButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
and do this
- (IBAction)actionEnter:(id)sender{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
In Swift:
button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])
I think, you could set the image in the beginning for normal and selected state ..
Try with below when you create the UIButton object. [Use the images as per your requirement]
[myButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
#7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.
In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.
self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite-Selected"]
forState:UIControlStateSelected];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite"]
forState:UIControlStateNormal];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore-Selected"]
forState:UIControlStateSelected];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore"]
forState:UIControlStateNormal];
If you are just toggling a button on or off, you won’t need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.
- (void)favoriteIgnore:(UIButton *)buttonPressed {
// Toggle the tapped button
buttonPressed.selected = ( buttonPressed.selected) ? NO : YES;
id <ScoringToolbarDelegate> TB_delegate = _delegate;
// Turn off the other button and call the delegate
if ([buttonPressed.currentTitle isEqualToString:#"favorite"]) {
self.ignoreButton.selected = NO;
[TB_delegate favoriteButtonPressed];
} else {
self.favoriteButton.selected = NO;
[TB_delegate ignoreButtonPressed];
}
}
to change the image immediately use the backgroundImage property.

Resources