Multi buttons to change value for UItextfield - ios

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

Related

Adding multiple UIButtons to an UIView with a selector action

I've added some buttons to an UIView (via addSubview) programmatically. I’ve added to this button an #selector with a function. However, they appear in the view but when I do click, the last button works only.
Into my .h:
#property (nonatomic, strong) UIButton * myButton;
Into my .m
for(int i=0;i<5;i++){
myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton addTarget:self action:#selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:myButton];
}
-(void)myaction:(UIButton *)sender{
if(sender.tag == 0){
NSLog(#“uibutton clicked %ld", (long)sender.tag);
}
}
How do I add the action to all buttons? not for the last only...
This works fine:
- (void)viewDidLoad {
[super viewDidLoad];
for(int i=0;i<5;i++){
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%ld", (long)i] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
}
-(void)myaction:(UIButton *)sender{
NSLog(#"uibutton clicked %ld", (long)sender.tag);
}
The code you posted in your question appears to be "this is what my code looks like" rather than your actual code. That can cause problems when people try to help.
In the future, post your actual code.
Let's make it more dynamic by calculating the height of the button and adding padding based on the number of buttons.
:
-(void)viewDidLoad {
[super viewDidLoad];
NSInteger height = self.frame.size.height - 5*20; // 5 is the button count
and 20 is the padding
NSInteger buttonHeight = height/5;
for(int i=0;i<5;i++){
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, buttonHeight*i+padding*(i+1), 150,
buttonHeight);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%ld", (long)i]
forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myaction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}}
-(void)myaction:(UIButton *)sender{
NSLog(#"uibutton clicked %ld", (long)sender.tag);
}
You could make it more dynamic by calculating the height of the button like this:
NSInteger height = self.frame.size.height - 5*20;
NSInteger buttonHeight = height/5;

How to check what buttons are selected and display all of them in a text field?

I am new at programming in Objective-C. I need some advice on how to allow the user to select 5 options out of 50 different ones (I will be using UIButton for that) and then display it in the UITextField for the user to see, and also check if it matches any results to qualify.
In a way it's like a card game, but using buttons for cards. I also need to make sure all 5 buttons are selected in order to display the result. I hope it's clear enough and someone could give me an idea or put me in the right direction on how to achieve it.
what if you give each button a tag, and give them all the same selector when the are tapped. And in the selector you can check to make sure the correct button was pressed based on what the senders tag is?
#define BUTTON_TAG_1 1
#define BUTTON_TAG_2 2
-(void)makeButton1{
CGRect frame = CGRectMake(0, 0, 40, 20);
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = frame;
[button1 setTitle:#"Button 1" forState:UIControlStateNormal];
button1.tag = BUTTON_TAG_1;
[button1 addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
-(void)makeButton2{
CGRect frame = CGRectMake(0, 20, 40, 20);
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = frame;
[button2 setTitle:#"Button 2" forState:UIControlStateNormal];
button2.tag = BUTTON_TAG_2;
[button2 addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
-(void)action:(UIButton*)button{
NSInteger buttonTag = button.tag;
textField.text = [NSString stringWithFormat:#"%i",buttonCounter++];
switch (buttonTag) {
case BUTTON_TAG_1:
//do somthing
break;
case BUTTON_TAG_2:
//do somthing else
break;
default:
break;
}
}

how to check UIButton image conditions

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

Fetching the tag values on selector

I am working with the iphone project. In that i added 3 buttons with same selector name(i.e action). Now i am fetch the data from the database using the query. But it is showing me the data to 3rd button only when i am pressing any butoon out of three .
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 30, 200, 50);
[btn.layer setBorderWidth:0];
btn.tag = 1;
[btn setTitle:#"1" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(detail:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont fontWithName:#"Zapfino" size:14.0];
[scrollview addSubview:btn];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 30, 200, 50);
[btn.layer setBorderWidth:0];
btn.tag = 2;
[btn setTitle:#"2" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(detail:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont fontWithName:#"Zapfino" size:14.0];
[scrollview addSubview:btn];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80, 30, 200, 50);
[btn.layer setBorderWidth:0];
btn.tag = 3;
[btn setTitle:#"3" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(detail:) forControlEvents:UIControlEventTouchUpInside];
btn.titleLabel.font = [UIFont fontWithName:#"Zapfino" size:14.0];
[scrollview addSubview:btn];
this is the action which i am using.
-(IBAction)detail:(id)sender
{
detailViewController *detailvc =[[detailViewController alloc]initWithNibName:#"detailViewController" bundle:Nil];
detailvc.btntxt = btn.tag;
NSLog(#"name of btn :%ld",(long)btn.tag);
[self.navigationController pushViewController:detailvc animated:YES];
}
In nslog also i am getting the tab of 3rd button only
please help me out regarding this issue...
You are assigning 1 tag to a button
btn.tag = 1;
add different tag to each button
--
Also in your detail: take the tag from the sender and not your instance variable
- (IBAction)detail:(UIButton *)sender {
detailViewController *detailvc =[[detailViewController alloc]initWithNibName:#"detailViewController" bundle:Nil];
detailvc.btntxt = sender.tag;
NSLog(#"name of btn :%ld",(long) sender.tag);
[self.navigationController pushViewController:detailvc animated:YES];
}
You set the tag number of all 3 buttons to the same value (1). You need to use different values if you want to be able to tell your buttons apart.
Use tag 1 for the first button, tag 2 for the second, and tag 3 for the third.
Use the sender variable to get the button that triggered the action:
-(IBAction)detail:(id)sender
{
UIButton *button = (UIButton *)sender;
NSInteger buttonTag = button.tag;
// etc
}
Also, give each button it's own tag
btn.tag = 2; // and 3
And don't use the btn variable in the action method. It will only ever contain the last set variable, which is the third button in this case.
just use ((UIButton)sender).tag instead of btn.tag
The main problem with your code is that you are using just single button object ( & initializing it three times & adding three different tags & adding it to subviews.), which looks like an ivar.
So even if you have added three different tags to a single object instance the recent most will be reflected & so you are getting just 3 as tag, all time.
btn = [UIButton buttonWithType:UIButtonTypeCustom];
....
btn.tag = 1;
.
.
btn = [UIButton buttonWithType:UIButtonTypeCustom];
...
btn.tag = 2;
.
.
btn = [UIButton buttonWithType:UIButtonTypeCustom];
...
btn.tag = 3
Instead you either don't use btn object in ur target method or you can have three different buttons for a good understanding.
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(80, 30, 200, 50);
....
btn1.tag = 1;
.
.
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(80, 80, 200, 50);
...
btn2.tag = 2;
.
.
UIButton* btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(80, 130, 200, 50);
...
btn3.tag = 3
Rest you need to modify the code of the target method as
-(IBAction)detail:(UIButton *)sender
{
detailViewController *detailvc =[[detailViewController alloc]initWithNibName:#"detailViewController" bundle:Nil];
detailvc.btntxt = sender.tag;
NSLog(#"name of btn :%ld",(long)sender.tag);
[self.navigationController pushViewController:detailvc animated:YES];
}
I hope this will certainly help you.
-(IBAction)detail:(UIButton*)sender
{
if(sender.tag==1)
{
}
else if(sender.tag==2)
{
}
else if(sender.tag==3)
{
}
}

Change label when a button has been pressed

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

Resources