I have three button images in table view cell, I would like to check the condition between them. When I click button 1 means button 3 should not work. Next condition button 3 clicked means button 1 should not work. button 2 can select in all conditions.
ButtonImageSelected = [UIImage imageNamed:#"lblue.png"];
ButtonImage = [UIImage imageNamed:#"l.png"];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setBackgroundImage:ButtonImage forState:UIControlStateNormal];
button1.tag = 1;
[button1 setBackgroundImage:ButtonImageSelected forState:UIControlStateSelected];
[cell.contentView addSubview:button1];
ButtonImageSelected1 = [UIImage imageNamed:#"eblue.png"];
ButtonImage1 = [UIImage imageNamed:#"e.png"];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
button2.tag = 2;
[button2 setBackgroundImage:ButtonImage1 forState:UIControlStateNormal];
[button2 setBackgroundImage:ButtonImageSelected1 forState:UIControlStateSelected];
button2.userInteractionEnabled = YES;
[button2 addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:button2];
ButtonImageSelected2 = [UIImage imageNamed:#"vblue.png"];
ButtonImage2 = [UIImage imageNamed:#"v.png"];
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
button3.tag = 3;
[button3 setBackgroundImage:ButtonImage2 forState:UIControlStateNormal];
[button3 setBackgroundImage:ButtonImageSelected2 forState:UIControlStateSelected];
[button3 addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:button3];
-(void)select_id:(UIButton *) tempBtn {
if (tempBtn.tag == 1) {
[tempBtn setSelected:YES];
[button1 setBackgroundImage:ButtonImageSelected forState:UIControlStateSelected];
[button2 setBackgroundImage:ButtonImageSelected1 forState:UIControlStateSelected];
[button3 setBackgroundImage:ButtonImage2 forState:UIControlStateNormal];
[tempBtn setSelected:! tempBtn.selected];
}
}
From the above code .....
u didn't set the the target of button 1 ..
set the target of button1 then try it
hope it will work ...
You need to add a selector for all 3, you can add an additional method that is called for all of the common processes you will use in all three selectors. In those selectors you either put button.enabled = true; or button.enabled = false;
As an example, here is the button 1 selector.
-(void)button1Selector: (id)selector {
button3.enabled = false;
[self doOtherStuff];
}
Related
I am trying to have a checkbox using button and this is what I am doing:
int checkBoxWidth = foregroundHeight / 12.0;
int checkBoxHeight = foregroundHeight / 12.0;
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, checkBoxWidth, checkBoxHeight)];
//[checkBox setBackgroundImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
//[checkBox setBackgroundImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
[checkBox setImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
checkBox.layer.borderWidth = 1.0;
checkBox.layer.borderColor = [Utility primaryColor].CGColor;
checkBox.layer.cornerRadius = cornerRadius;
[switchView addSubview:checkBox];
but the image appears only when I touch the button, in any other state image is not showing up. Is there something missing?
You have to change the button state for the image to be changed. Use the below code to achieve your desired result.
int checkBoxWidth = foregroundHeight / 12.0;
int checkBoxHeight = foregroundHeight / 12.0;
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, checkBoxWidth, checkBoxHeight)];
[checkBox setImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateHighlighted];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected | UIControlStateHighlighted];
[checkBox addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
checkBox.layer.borderWidth = 1.0;
checkBox.layer.borderColor = [Utility primaryColor].CGColor;
checkBox.layer.cornerRadius = cornerRadius;
[switchView addSubview:checkBox];
And add this target function
-(IBAction)buttonClicked:(UIButton*)sender{
sender.selected = !sender.selected;
}
I have a button which changes images which it has been pressed, it goes from img1 to img2. I now also want the text to be changed when that certain button been pressed. So it starts on "hello", and then when pressed it turns to "goodbye" for example.
this is my code at the moment:
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"greypad.png"]) ? #"greenpad.png" : #"greypad.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
ViewDidLoad:
[self addLabel];
self.btn1ImageName = #"greypad.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(46, 67, 48, 48)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
The label?
-(void)addLabel{
UILabel *aLabel = [[UILabel alloc]initWithFrame: CGRectMake(20, 200, 280, 80)];
aLabel.numberOfLines = 0;
aLabel.textColor = [UIColor blueColor];
aLabel.backgroundColor = [UIColor clearColor];
aLabel.text = #"hello";
[self.view addSubview:aLabel];
}
Try this
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"greypad.png"]) ? #"greenpad.png" : #"greypad.png";
self.titleStr = ([self.titleStr isEqualToString:#"hello"]) ? #"goodbye" : #"hello";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 setTitle:self.titleStr forState:UIControlStateNormal];
}
I would recommend a different approach:
When you set up your button, set different title and image for different states
[btn1 setTitle:#"hello" forState: UIControlStateNormal];
[btn1 setTitle:#"goodbye" forState: UIControlStateSelected];
[btn1 setImage:[UIImage imageNamed:#"greypad.png"] forState:UIControlStateNormal];
[btn1 setImage:[UIImage imageNamed:#"greenpad.png"] forState:UIControlStateSelected];
And in your toggle method simply do
- (void)toggleImage:(UIButton *)button
{
button.selected = !button.selected;
}
Add this to your toggleImage Method:
[bt1 setTitle:#"goodbye" forState: UIControlStateNormal];
What would be the best practice to achieve the following without having to have multiple IBActions?
When one of the blue buttons are clicked i want it to change the input field below to the VALUE assigned by the button.
Thanks
You need to set UIButton tag for Button1, Button2... As 0, 1, and so on
EDIT
-(IBAction)buttonAction:(UIButton *)sender
{
switch(sender) {
case 0:
//Change UITextValue
break;
case 1:
//Change UITextValue
break;
default: //Not found
break;
}
}
U can also achieve this by setting the target to same selector
for example
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(20, 20, 100, 100);
button2.frame = CGRectMake(20, 130, 100, 100);
button1.tag = 100;
button1.backgroundColor = [UIColor greenColor];
button2.backgroundColor = [UIColor redColor];
button2.tag = 200;
[self.view addSubview:button1];
[self.view addSubview:button2];
//for selector
-(void)buttonTapped:(UIButton *)sender
{
NSLog(#"same target and tag =%d",sender.tag);
}
I want the picture to turn yellow the second the user touches it. Right now on first tap the image turns a bit grey and on release turns yellow.
Second tap the picture would go back to red instantly. How can i fix this?
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
I ran your code and it's working fine mate. Just double check the image names!
EDIT 1:
replace your code with the followings:
-(void)bMethod:(UIButton*)sender
{
if (sender.imageView.image != [UIImage imageNamed:#"red.png"]){
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"yellow.png"] forState:UIControlStateNormal];
}
else{
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"red.png"] forState:UIControlStateNormal];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchDown];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
}
EDIT 2:
-(void)bMethod:(UIButton*)sender
{
sender.selected = !sender.selected;
sender.highlighted = !sender.highlighted;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"red.png"];
UIImage *buttonImageSelected = [UIImage imageNamed:#"yellow.png"];
myButton.frame = CGRectMake(200, 200, 40, 40);
[myButton addTarget:self action:#selector(bMethod:) forControlEvents:UIControlEventTouchUpInside];
myButton.adjustsImageWhenHighlighted = NO;
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImageSelected forState:UIControlStateSelected];
[myButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
}
I coded this:
UIButton *buttonPrint = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonPrint setImage:[UIImage imageNamed:#"stampa.png"] forState:UIControlStateNormal];
buttonPrint.frame = CGRectMake(15, 5, 117, 41);
[buttonPrint addTarget:self action:#selector(print) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonPrint];
//button mail
UIButton *buttonMail = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonMail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
buttonMail.frame = CGRectMake(0, 46, 117, 41);
[buttonMail addTarget:self action:#selector(sendEmail) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonMail];
//button facebook
UIButton *buttonPublish = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonPublish setImage:[UIImage imageNamed:#"pubblica.png"] forState:UIControlStateNormal];
buttonPublish.frame = CGRectMake(20, 90, 117, 41);
[buttonPublish addTarget:self action:#selector(publish) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonPublish];
//button twitter
UIButton *buttonTweet = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonTweet setImage:[UIImage imageNamed:#"tweet.png"] forState:UIControlStateNormal];
buttonTweet.frame = CGRectMake(4, 130, 117, 41);
[buttonTweet addTarget:self action:#selector(tweet) forControlEvents:UIControlEventTouchUpInside];
[viewBack addSubview:buttonTweet];
- (void)sendEmail {
NSLog(#"mail clicked");
}
- (void)tweet {
NSLog(#"tweetClicked");
}
- (void)print {
NSLog(#"printClicked");
}
- (void)publish {
NSLog(#"face clicked");
}
but the buttons does not work... What should i do??
UIImageView has a property userInteractionEnabled. It's disabled by the default, so if you are adding youre buttons as a subviews of the image view they will not respond. If this is your issue. you can enable that flag or consider to move the buttons to the image view's superview.
Hope it helps. Cheers.