image change when click another button - iphone

I am writing code for a puzzle matching game. I defined a UIButton with an UIImage, I added a target to change the UIImage of a UIButton.
I want this photo to appear until I click another UIButton or the same UIButton, how can I do that ?!
the UIButton
mybutton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton1 addTarget:self
action:#selector(method:)
forControlEvents:UIControlEventTouchDown];
question = [UIImage imageNamed:#"puzzle.jpg"];
[mybutton1 setBackgroundImage:question
forState:UIControlStateNormal];
then the method:
-(void) method: (UIButton*) sender{
[sender setBackgroundImage:[UIImage imageNamed: #"cheerful-cat.jpg"]
forState:UIControlStateNormal];
}

Try this way..
Button* mybutton1= [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton1 addTarget:self
action:#selector(method:)
forControlEvents:UIControlEventTouchDown];
question = [UIImage imageNamed:#"puzzle.jpg"];
[mybutton1 setBackgroundImage:question
forState:UIControlStateNormal];
-(void) method: (UIButton*) sender{
[sender setBackgroundImage:[UIImage imageNamed: #"cheerful-cat.jpg"]
forState:UIControlStateNormal];
}

Related

How do I get a UIButton to change its color by clicking it?

I want to make an app for iphone. It is a Nonogram something similar to this: http://www.puzzle-nonograms.com/ How can I make the UIButtons changes their color from white to black and reverse?
.h file
UIButton *button;
BOOL clicked;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
clicked=YES;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(changeBtnColor)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#" 1 " forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)changeBtnColor{
if (clicked) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
clicked=NO;
}
else{
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
clicked=YES;
}
}
I hope it will help. Best of luck..
No need to ivar a boolean, buttons have selected, highlighted and normal states built into them.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 320, 50)];
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Normal State" forState:UIControlStateNormal];
[button setTitle:#"Selected State" forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"normal_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[self.view addSubview:button];
}
-(void)buttonPress:(UIButton *)button{
button.selected = !button.selected;
//do your other code here for button selection
}
Otherwise set up the button states inside interface builder and just have the buttonPress selector handle the state change.

Overwriting an image button with another image when I click that button

I have created an image button (a.png) infront of each row in the cellForRow method. In the same method itself I'm calling a buttonPressed method which has the code to be executed when the button is pressed. And I want that on the pressing of the button, the button image should be changed to another image (b.png) of the same size.
Please help with how to do that.
[but setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
try this......
Create Custom Cell for UITableView and UIButton on Custom Cell.
Create property for UIButtion.
In cellForRowAtIndexPath: method
//Set Action on Button in Table View Row
[cell.UIButtonOutlet addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
This will create action from your button.
And in method
-(void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches=[event allTouches];
UITouch *touch=[touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.historyTableView];
//GET IndexPath
NSIndexPath *indexPath=[self.TableView indexPathForRowAtPoint: currentTouchPosition];
selectedCell= [PairTableView cellForRowAtIndexPath:indexPath];
[selectedCell.UIButtonOutlet setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Try the following method
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button setFrame:CGRectMake(12,12,200,200)];
[button addTarget:self action:#selector(click: withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:button];
then in the click action method of button
-(void)click:(UIButton *)button :withEvent:(UIEvent *)events{
[self yourFunction:(UIButton *)button];
}
then finally
-(void)yourFunction:(UIButton *)button{
button.selected = ! button.selected;
if (button.selected) {
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
}
else{
[button setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
}
}
Try this when initializing your button
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Then modify your method(which is called when button is pressed) like this
-(void)yourMethod:(id)sender{
UIButton *btn=(UIButton*)sender;
[btn setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
//your other code
}

button image change using predefined method

In my app am using UIScrollView. I added buttons to UIScrollView.
For each button i assigned image using
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
and actions.
forState i used UIControlStateNormal in viewDidLoad.
And when we click on button image has to change.
For this i used same method but forState value changed to UIControlStateSelected in ViewDidLoad.
But button image is not changing.
any one can help or suggest
You've to use like this
In ViewDidLoad Set images for normal and selected state
-(void)ViewDidLaod
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateSelected];
To perform selection image change can be done below code
-(void)buttonclick
[myButton setSelected:YES];
or
[myButton setSelected:NO];
Use this line of code I think this will be helpful for you.
-(void)viewDidLoad
{
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
}
-(void)compete
{
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateNormal];
}
use
[button setImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"button_highlighed.png"] forState:UIControlStateHighlighted];

Add UIButton programmatically from a IBAction

I am trying to make an action where every time you press a button, it adds a button to the view. Nothing I have tried works.
It would be much appreciated if someone could clear up what to do.
This is the most recent attempt at my button-adding button:
-(IBAction) addButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self
action:#selector(buttonTouched:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Try the above codes.
you need to add this
#import <QuartzCore/CoreAnimation.h>
then Do the follow this
-(IBAction)buttonTouched:(id)sender{
NSLog(#"New Button Clicked");
}
-(IBAction)addButton:(id)sender
{
UIButton *btnallstates;
UIImage *statesbtnimg=[UIImage imageNamed:#"1.png"];
btnallstates=[[UIButton buttonWithType:UIButtonTypeCustom]retain];
btnallstates.tag=1;
btnallstates.frame=CGRectMake(103, 127, 193, 31);
[btnallstates.layer setBorderWidth:0];
[btnallstates addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[btnallstates setBackgroundImage:statesbtnimg forState:UIControlStateNormal];
[self.view addSubview:btnallstates];
}
Your code is correct. Only change this line
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
to
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
to show the button on the view.
UIButtonTypeCustom does not show up although it is added to the view.
Your can understand this by changing the color of the button.
e.g change the code to this.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor redColor];
Hope it works. Enjoy.
Try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

Title for UIButton won't appear

So I am trying to create a custom UIBarButtonItem, but I can't for the life of me figure out why the title won't show. Maybe someone else can spot it :\
Here is the code I use to create the bar button:
+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title normal:(UIImage *)normal highlighted:(UIImage *)highlight selected:(UIImage *)selected target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0.0f, 0.0f, normal.size.width, normal.size.height)];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
[button setImage:selected forState:UIControlStateSelected];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:#"Cancel"
normal:[UIImage imageNamed:#"back_button_active.png"]
highlighted:[UIImage imageNamed:#"back_button_highlight.png"]
selected:[UIImage imageNamed:#"back_button_selected.png"]
target:self
action:#selector(popViewController)];
Any help would be appreciated!
P.S. This is in iOS 5 using ARC.
title and image are mutually exclusive properties in UIButton. If you want to have title with custom background then you should use backgroundImage property:
...
[button setBackgroundImage:normal forState:UIControlStateNormal];
[button setBackgroundImage:selected forState:UIControlStateHighlighted];
[button setBackgroundImage:selected forState:UIControlStateSelected];
...