i need to place a check box in UItableview.
For that i place a box with out check image is place on button.
my code is,
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=CGRectMake(100, 10, 100, 100);
[customButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:customButton];
Now i need to change button image when button clicked.
where can i done this.
and how can i impalement this.
can any one please help me.
Thank u in advance.
//add this line
[customButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//then
-(void) buttonClicked:(id) sender
{
UIButton * button = (UIButton*) sender;
[button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
You can write this line of code
[customButton setImage:[UIImage imageNamed:#"unselected2.png"] forState:UIControlEventTouchDown];
before
[cell.contentView addSubview:customButton];
and you ca select different images for different events and states
Related
Currently i am working in iPhone application, Using two buttons, then i set the image both button1 and button2, then i tried to drag a image from button1 to button2, Is it possible to do this? please help me
Thanks in Advance
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"hearta.png"] forState:UIControlStateNormal];
button1.frame=CGRectMake(10, 60, 50, 50);
[button1 addTarget:self action:#selector(Button1Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"club4.png"] forState:UIControlStateNormal];
button2.frame=CGRectMake(61, 60, 50, 50);
[button2 addTarget:self action:#selector(Button2Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2]
You can get reference from this demo project.
it's very nice.
Also another link is here
There are more than one ways of doing this ...
You can either create a UIImageView on the click event or action of button1 and use animation to drag that image till button2 where you make the imageview.image equal to button2's image , side by side making the original imageview as nil so that you can show it disappear into button2.
Also, instead of buttons you can use UIImageViews directly and make and animation effect on the click methods there too.
Currently i am working in iPhone application, I have create two button and set image for both buttons, when the user touch button2 then show button1 image inside button2, i tried my level best, but i can't fix this, please any one help me
Thanks in Advance
I tried this:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"4.png"] forState:UIControlStateNormal];
btn1.frame=CGRectMake(61, 60, 50, 50);
[btn1 addTarget:self action:#selector(Button1Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"8.png"] forState:UIControlStateNormal];
btn2.frame=CGRectMake(112, 60, 50, 50);
[btn2 addTarget:self action:#selector(Button2Method) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
-(void)Button2Method
{
[btn2 setImage:[UIImage imageNamed:btn1.currentBackgroundImage] forState:UIControlStateNormal];
}
Try this:-
[btn2 setImage:btn1.currentImage forState:UIControlStateNormal];
And currentBackgroundImage is an UIImage and you're treating it as a NSString this is wrong.
Use this
UIImage *img = btn1.currentImage;
[btn2 setImage:img forState:UIControlStateNormal];
This is because you are setting image to button btn1 setImage: and accessing background image btn1.currentBackgroundImage
so change your code Tested
-(void)Button2Method
{
[btn2 setImage:btn1.imageView.image forState:UIControlStateNormal];
}
I have an UIButton, I need to change button's image when button is tapped. Button's background images are set in ViewDidLoad method. Pls refer images attached on this thread.
When I tap on button first time, button property will be changed to selected and "arrow_right.png" image will be set to our Button.
2.On second tap, I observed that when button state is highlighted, But it's background image is not setting. You can see some blur effect instead.
My concern is I saw while toggling UIButton, image is not getting set for highlighted state. when button's state changing from Selected to Normal.
Is it a bug or my mistake?
Thank you.
- (void)viewDidLoad
{
[super viewDidLoad];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_normal.png"] forState:UIControlStateNormal];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateSelected];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateHighlighted];
}
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
button.selected = !button.selected;
if (button.selected)
{
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateSelected];
}
}
try this
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
if(button.selected) {
[button setSelected:NO]
} else {
[button setSelected:YES]
}
}
i diddn't understand why you need to set image again, all you need to do is set its selected property , button will behave as you expected
I am a new iPad developer.
I have created UIButton programmatically, in which I want to identify user has clicked on which button and according to that I want to do some action.
How should I identify this?
Here is my code snippet:
for(int i=0;i<[_array count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=count;
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.backgroundColor=[UIColor redColor];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(xpos, 270.0, 200.0, 150.0);
[self.view addSubview:button];
}
This is what I'm doing now: I thought I will assign count to each button and I will pass this count to button click method. Is this a good idea? Is there any other way possible?
On button click I'm calling aMethod:
-(void)aMethod:(id)sender{
NSLog(#"btn clicked");
}
Any help will be appreciated!
-(void)aMethod:(UIButton*)sender{
NSLog(#"btn clicked %d", sender.tag);
}
I have something like this...
for(int i=0;i<10;i++){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button1" forState:UIControlStateNormal];
button.frame = CGRectMake(00.0, 00.0, 100.0, 30.0);
[view addSubview:button];
}
It's possible to instantiate each button with his own id? For example button+i ?
Thanks in advance!
You can set a numeric id to each button using the tag property:
button.tag = i;
You can then get the button instance afterwards with the code
[view viewWithTag:i];
provided you have added it to a view, like using [view addSubview:button].