i draged 3 button in my .xib file (btn1,btn2,btn3 respectively) and initially i given default image to them, first.png
now when user clicks on btn1, image of btn1 should change from first.png to second.png..
and when user selects on btn2, image of btn2 should change from first.png to second.png, and also change image of btn1 to default again first.png, so that user came to know he has clicked 2nd button.
how should i do this ?
Thanks In Advance !!
You have to set button action method code
-(IBAction)btnClked:(id)sender
{
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn3 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
}
#import <Foundation/Foundation.h>
#interface CustomRadioButton : UIButton {
}
#end
#import "CustomRadioButton.h"
#implementation CustomRadioButton
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Set the refresh icon as the button's image
[self setImage:[UIImage imageNamed:#"off.png"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:#"on.png"] forState:UIControlStateSelected];
// When the button is pressed, draw to button with less opacity.
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
#end
inside the Implement file of the viewController
- (void)viewDidLoad {
[super viewDidLoad];
for (int i=1;i<=2;i++){
CustomRadioButton *Radiobutton = [CustomRadioButton buttonWithType:UIButtonTypeCustom];
Radiobutton = [[CustomRadioButton alloc] initWithFrame:CGRectMake(200,50*i, 30, 30)];
[Radiobutton addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
Radiobutton.tag=i;
[self.view addSubview:Radiobutton];
}
}
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Set the images in your button call method like:
BOOL first;
-(void)firstBtnPressed
{
if(first == YES){
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
first = NO;
}
else
{
first = YES;
[btn1 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
}
}
Related
My program execute the buttonPressed method and go through the if else statement but the text on my button still does not changed. Is there something I had missed? I even already customized it in xib but the text of the buttonPlayMusic is remained as "Play" even it goes into the other statement.
I think I had connected the button too.
[viewDidLoad]
{
self.buttonPlayMusic = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.buttonPlayMusic.frame = CGRectMake(((scrollView.frame.size.width - 200) / 2) + cx, 360, 200, 50);
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.toggleButonPlayMusic = YES; //YES to play, NO to stop music
[scrollView addSubview:self.buttonPlayMusic];
}
-(IBAction)buttonPressed:(id)sender{
NSLog(#"SSS");
if (self.toggleButonPlayMusic == YES) {
[self.buttonPlayMusic setTitle:#"Stop" forState:UIControlStateNormal];
self.toggleButonPlayMusic = NO;
NSLog(#"RRR");
}else{
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
self.toggleButonPlayMusic = YES;
NSLog(#"ZZZ");
}
}
no need to take extra boolian variable just put this code and it worked fine
-(void)viewDidLoad
{
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
self.buttonPlayMusic = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.buttonPlayMusic.frame = CGRectMake(((scrollView.frame.size.width - 200) / 2) + cx, 360, 200, 50);
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonPlayMusic setSelected:TRUE]; //YES to play, NO to stop music
[scrollView addSubview:self.buttonPlayMusic];
}
-(IBAction)buttonPressed:(id)sender
{
if (self.buttonPlayMusic.selected == TRUE)
{
[self.buttonPlayMusic setTitle:#"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(#"RRR");
}
else
{
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(#"ZZZ");
}
}
Make sure that you are dragging a reference from the file to your viewController using ctrl drag and putting it in your #interface (sometimes you can accidentally get rid of these). This post has some methods for making sure the connection is set up properly.
Try this
-(IBAction)buttonPressed:(id)sender
{
UIButton *btn =(UIButton*)sender;
if (self.buttonPlayMusic.selected == TRUE)
{
[btn setTitle:#"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(#"RRR");
}
else
{
[btn setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(#"ZZZ");
}
}
-(IBAction)buttonPressed:(id)sender
{
if (self.buttonPlayMusic.titleLabel.text == #"Play")
{
[self.buttonPlayMusic setTitle:#"Stop" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:FALSE];
NSLog(#"RRR");
}
else if (self.buttonPlayMusic.titleLabel.text == #"Stop")
{
[self.buttonPlayMusic setTitle:#"Play" forState:UIControlStateNormal];
[self.buttonPlayMusic setSelected:TRUE];
NSLog(#"ZZZ");
}
}
I am created two buttons that are right next to each other to mimic a segmented control. I am doing this to customize the appearance beyond what the UIKit allows. I decided to use the selected property to keep a button pressed. I have two images that one for each state normal and selected.
The problem is that when I select a button, the button highlights and turns dark, because of the hightlight state. I decided to use the selected image for the highlight state too, but it flashes, any ideas or suggestions.
- (void)leftSegmentPressed:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
For the "selected" button, disable it and manually switch the image for the state.
- (void) viewDidLoad
{
[rightSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[rightSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
[leftSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[leftSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
}
- (void)leftSegmentPressed:(id)sender
{
sender.enabled = NO;
rightSegmentButton.enabled = YES;
}
- (void)rightSegmentPressed:(id)sender
{
sender.enabled = NO;
leftSegmentButton.enabled = YES;
}
Check whether the image you given is in your Bundle or check image name you given is in lower case or not. Then write like
[button1 setImage:[UIImage imageNamed:#"normal1.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"normal2.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateSelected];
button1.tag = 1;
button2.tag = 2;
[button1 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
[button2 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
in your button event method
-(void)buttonSelected:(id)sender {
if([sender tag] == 1) {
button1.selected = YES;
button2.selected = NO;
} else {
button1.selected = NO;
button2.selected = YES;
}
}
[button setAdjustsImageWhenHighlighted:NO];
This will prevent the flicker.
UIButton *yourButton1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
yourButton1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[yourButton1 setTitle:#"Left" forState:UIControlStateNormal];
yourButton.backgroundColor = [UIColor clearColor];
yourButton1.tag = 1;
[yourButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"yourNormalImage.png"];// set normal image
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"yourSelectedImage.png"];// set selected image
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[yourButton1 addTarget:self action:#selector(leftSegmentPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton1];
do same for another second button.
and in action method set bellow code..
- (void)leftSegmentPressed:(id)sender
{
UIButton *btnTemp = (UIBUtton *)sender;
if (btnTemp.tag == 1) {
[yourButton1 setSelected:YES];
[yourButton2 setSelected:NO];
}
else {
[yourButton1 setSelected:NO];
[yourButton2 setSelected:YES];
}
}
i am creating an application which includes dynamic pages. actually, i created some dynamic questions and UIButtons (custom checkBox) by checking the responses from the server.
my code is here,
NSString *response=[ResponseFromServer ObjectAtIndex:0];
if ([comSt compare: #"CheckBoxList"]==NSOrderedSame)
{
int count=[ResponseFromServer count];
for(int i=0;i<count;i++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=i;
[button setFrame:CGRectMake(0.0f, 0.0f, 37, 37)];
[button setCenter:CGPointMake(116.0,p1)];
[button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor whiteColor]];
[button addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
p1=p1+30;
}
-(void)checkButtonTapped:(id)sender
{
int tagVal = [(UIButton*)sender tag];
if (tagVal==0) {
NSLog(#"tag = 0");
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"check.png" forState:UIControlStateSelected]; //not working, button image is not changing
}
assume i have created 5 dynamic buttons with different tag. i set the image for all those button to uncheck.png first. it works.
but when i clicked a specific button and need to change that button image to checked.png, its not working on the button action.
i need to change it like checkBox control, how will i access the control of a dynamically created button from a group of buttons?
i think u can understand my requirement, thanks in advance..
While adding dynamic button set [button setSelected:NO];
Replace method and Add one method given below
-(void)checkButtonTapped:(id)sender
{
UIButton *curButton = (UIButton*)sender
if (curButton.tag==0)
{
[self buttonAction:curButton];
}
if (curButton.tag==1)
{
[self buttonAction:curButton];
}
// continues
}
-(void)buttonAction:(UIButton *)curButton
{
if(![curButton isSelected])
{
[curButton setSelected:YES];
[curButton setImage:[UIImage imageNamed:#"check.png" forState:UIControlStateSelected]; //not working, button image is not changing
} else {
[curButton setSelected:NO];
[curButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
}
In your code you need to change like this
Try this code
if ([comSt compare: #"CheckBoxList"]==NSOrderedSame)
{
int count=[ResponseFromServer count];
for(int i=0;i<count;i++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=0;
[button setFrame:CGRectMake(0.0f, 0.0f, 37, 37)];
[button setCenter:CGPointMake(116.0,p1)];
[button setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor whiteColor]];
[button addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
p1=p1+30;
}
}
-(void)checkButtonTapped:(id)sender
{
int tagVal = [(UIButton*)sender tag];
if (tagVal==0) {
NSLog(#"tag = 0");
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"check.png" forState:UIControlStateSelected];
[button setTag:100];
}
else
{
[button setSelected:YES];
[button setImage:[UIImage imageNamed:#"uncheck.png" forState:UIControlStateSelected];
[button setTag:0];
}
}
I have dynamically created some list of RadioButtons with some values. pro grammatically I changed the button state and changed the image for selected and unselected. but the problem is i can select the all radioButtons at same time. actually I need to select one at a time.
when I clicked the next RadioButton, previously selected button state should be changed to non-selected.
Here is my code, I tried with changing image, but ...some problem with my code.
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender ;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
where can I change the image of previously selected button.
thanks in advance
Deselect all the buttons when you select one. If you have buttons in your scroll view you can user this code:
//Your Method
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender;
[self deselectAll];
[self radiobuttonAction:RadioButton1];
}
- (void) deselectAll : (UIScrollView *) scrollView{
NSArray *viewArray = [scrollView subviews];
for (UIView *v in viewArray){
if([v isKindOfClass:[UIButton class]]){
[((UIButton *)v) setSelected:NO];
}
}
}
EDIT: But if you want to give it real radio button effect (in which one is always selected, and only one is selected) it will be more easy to you. Use following code:
//A globle refButton
UIButton *refButton = nil;
//Set image for both state:
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateSelected];
//make any of above default select: may be the last one and pass that to `refButton`
refButton = RadioButton;
//Your Method
-(void)RadioButtonTapped:(id)sender
{
[refButton setSelected:NO];
refButton = (UIButton*)sender;
[refButton setSelected:YES];
}
Tag the button at the time of creation.
When button tapped, get the buttons through tag. Deselect all buttons. Update current button which you have received as a n argument.
assuming you want to add 5 radio buttons.
- (void)viewDidLoad
{
for (int i = 0; i < 5; i++) {
UIButton* RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0, i * 40)];
// [RadioButton setTag:i * 10];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
}
//do not forget to declared this in your header file
-(void)RadioButtonTapped:(UIButton*)button;
{
for (UIButton *btn in self.scrollView.subviews) {
[btn setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
}
[button setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
the code above updates the UI, sets the image of all buttons to "uncheck" then changes the image of the last pressed button to "check".
create buttons like this,
for(int i=0;i<4;i++)
{
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
RadioButton.tag = i*100;
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
add action like this,
-(void)radiobuttonAction:(UIButton *)Button
{
UIButton *Button = (UIButton*)sender;
for(UIButton * btn in self.scrollview.subViews)
{
if(btn.tag == Button.tag)
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
else
{
//do selected or de-selected code for other buttons
}
}
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"restaurant.png"] forState:UIControlStateNormal];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"restaurant1.png"] forState:UIControlStateNormal];
if([scaleStr isEqualToString:#"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:#"0.209607"]) {
[btn1 removeFromSuperView];
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
i have set the button when doubletap the background image in UIScrollview.If i double tap second time , the first button also show.I need to remove the button which i created first .
Also i remove the btn1 in next condition.
Kindly help me regarding on this.
A little bit of context might help here, but my guess is that you're really just creating a new button each time and not holding a reference to the old one. So when you call removeFromSuperView on the second tap, you're removing a button that hasn't even been added to the view, not the one you added on your last pass. Try making your buttons instance variables instead of local. In extremely loose pseudo-code:
#interface MyClass : UIView {
UIButton *btn1;
UIButton *btn2;
}
#implementation MyClass {
-(MyClass *) init {
self = [super init];
[self setupButtons];
return self;
}
-(void) setupButtons {
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"restaurant.png"] forState:UIControlStateNormal];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"restaurant1.png"] forState:UIControlStateNormal];
}
-(void) myFunction {
[btn1 removeFromSuperView];
[btn2 removeFromSuperView];
if([scaleStr isEqualToString:#"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:#"0.209607"]) {
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
}
}