iOS Determine button that was programmatically created - iphone

I have made a horizontal scroll view with images and buttons in it using a for loop, what i want to do now is when a button is pressed, open the image it corresponds to full screen. The issue im having is determining which button has been pushed. I am using :
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then :
-(void)buttonPressed:(UIButton *)sender {}
What can i do to fix this? Thanks

Which button has been pushed
Create a property to store a tag. In the buttonPressed method set the property to sender.tag
Now you can know which was the button who got pressed last.

The sender is the button that the user has tapped, so when you create the buttons you could use the tag property and set it to an index of an array where you hold your images. But this could be a bit unstable if you change the order or amount of images or buttons for example so be prepared to check for that.

one way to do so is to keep references of your button (with a property, an attribute in your class) and test if it is the good one in your buttonPressed method
- (void)buttonPressed:(UIButton *)sender {
if (sender == self.myButton) {
// DO YOUR WORK HERE
}
}
you can also create a method for this and only this one button
by the way it is better to say
- (IBAction)buttonPressed:(UIButton *)sender
you then can set the target of your button in the interface builder interface

set tag for each button in the for loop but.tag=i;
-(void)buttonPressed:(UIButton *)sender {
if (sender.tag==1){
//display image 1
}
else if ....
}

Related

How to pass UIAction without touching the UIButton

I've got a UIButton that acts as a switch.
When user taps on it, its state changes to "selected" and action "one" is called. When user taps again UIButton state changes to "not selected" and the action is no longer available.
Is there a way to set the UIButton to "selected" by taping on a completely different UIButton and have it change to "selected" and call the same action as well?
Cheers
From what I have understood from the question, I would like to give you my suggestion
if (!<your_button>.selected)
{
<your_button>.selected = YES;
// do your stuff here
}
else
{
<your_button>.selected = NO;
// do your stuff here, in your case action should be no longer available so that,
<your_button>.userInteractionEnabled = NO;
}
Enjoy Programming !
try like this,'
-(IBAction)nextbutton:(id)sender{
UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
btn.selected=YES;
//do whatevr you want.
}
simple thing when you tap on the uibutton make an action for that and in the action for that button , you can set the button state to selected and then in the next line of code you can call that method (action)
- (IBAction)btn1Touched:(id)sender {
[_btn2 setSelected:YES];
[self btn2Touched:self];
}
- (IBAction)btn2Touched:(id)sender {
NSLog(#"btn Action called");
}
UIbutton has state called selected and it is managed via property selected
so to make a button to selected state
[buttonInstance setSelected:YES];
To make it unselected use
[buttonInstance setSelected:NO];
Kindly write a function for that button action like,
-(void)Buttonaction
{
do your action here
also set your button has selected state.
buttonname.setselected=yes;
}
call this function while you are tabbing your button.
Do This:
-(IBAction)switchButtonClicked:(UIButton*)sender
{
if(sender.isSelected)
{
.....
}
else
{
...
}
[sender setSelected:!sender.isSelected];//This will work as Switch.
}
set Selected(Switch ON) and default(Switch OFF) property (image and title) of UIButton from XIB, OR from Code as you want.
With a single button you can do you functionality with this code

How to identify which button was clicked

I have a Button, Once the user submits a form the button image should change to DONE.PNG, or else it will remain as SUBMIT.PNG.
I need to know the following;
1.) How can we write a method, to know which button the user clicked. (If he clicked the button when it has the DONE.PNG or SUBMIT.PNG image on it)
my button click event is -(void)buttonClicked : (id)sender {}
Normally you would set the tag of the UIButton.
-Interface or storyboard you do it under the info about the UIButton element.
-Programatically you do it like this : myButton.tag = 23;
Then in the buttonClicked you do this:
UIButton* senderButton = (UIButton*) sender;
if(senderButton.tag==23) {
// It's the button as submit
// Set button image
senderButton.tag = 5;
}
if(senderButton.tag==5) {
// Button is done
}
Hope you get it working :)
As you are using a single button and changing its image only, then there are two simple ways that you can follow:
Check the name of image of the button if its DONE.PNG then do what is required and change the image else vis-versa.
Have a variable either you can take integer(it will help you if you have even more number of image changes on same button) to track button state/image.
For example we will typedef buttonState and check against it for suitable case.
In .h file
//Before interface declaration..
typedef enum
{
ButtonStateDone = 1,
ButtonStateSubmit,
//any other state that it may have.
} ButtonState
//in interface declaration..
ButtonState buttonState; //its a class level variable that we will use to track button state.
In .m file
Initially set buttonState as you show it on initial view. suppose button shows DONE.PNG
so buttonState = ButtonStateDone;
Now, in button action you will change this
if(buttonState == ButtonStateDone)
{
//do something and change button state.
}
if(buttonState == ButtonStateSubmit)
{
//do something and change button state.
}
Here, I used typedef, it is useful if you need to have more than 2 states for button else you can simply use a BOOL variable.
You can add a tag to the button i.e. NSInteger buttonTag = [sender tag] and then have conditional statements to check the tag values to determine which button was clicked.

UIButton Interchange with touch animation in IPhone?

I have 5X5 button in a view.
Based on some Condition, i want to interchange them.
How can i know which of them is touched, if move is not possible then a message
alert will be displayed.If the move is possible then current button should replace the
Previous one.
Thanks in Advance...
I suggest you review your question as it is difficult to understand what you really want. But to know which button was touched up inside you can assign unique tags to buttons and then check for the sender's tag. Or another way, declare 25 UIButton IBOutlets (instance variables) and connect them to the buttons in Interface Builder. And then you can check if the [sender isEqual:button1(button2 etc....)].
-(IBAction)buttonTouched:(UIButton*)sender{
if (sender.tag=#"Button1")
..........
}
or
IBOutlet UIButton button1;
..........
-(IBAction)buttonTouched:(UIButton*)sender{
if ([sender isEqual:button1])
.......
}
After clicking the button if you can retreive the title of the button you can use the follwing code(if your button touch down event is connected to this function):-
-(void)btnClicked:(id)sender {
UIButton *rButton = (UIButton *)sender;
NSLog(#" The button's title is %#." rButton.currentTitle);
}

How to get the name of the button in an action?

I have a button named start and I want to know in the method that it calls what it's name is and I'm not really sure how to do it. This is the method the button calls.
-(IBAction) startMotion: (id)sender {
UIButton * buttonName = (UIButton *) sender;
NSLog(#"Button Name: %#", buttonName.currentTitle);
}
The NSLog prints
Button Name: (null)
You can set the title of the button through
[b setTitle:#"Start" forState:UIControlStateNormal];
and to get the title (currentTitle is read-only and may be nil):
[b currentTitle];
BTW, if you just want to differentiate multiple buttons, you can just set the tag property (an integer value) of the buttons.
Also, check if you have the button specified as an IBOutlet in your viewController class, and is it connected properly as an outlet in Interface Builder?
I would rather set a certain Tag and compare the tag value rather than reading the title of the button since you have possibility to localize your app where button titles will possibly be different.
I was using the wrong property in Interface Builder.I was using name property of button in Interface Builder instead of the title property from the button settings.

iphone sdk button tags?

I can't understand the logic og button tags. Can someone tell me how to use button tags?
For eg. There are two buttons on my view and I want to print something depending on their tags like:
if(button.tag==???)x{
}etc.
When you create the button, you can set it's tag.
myButton1.tag = 0;
myButton2.tag = 1;
Or if you're using interface builder, there's a field in the inspector to set the tag.
I assume you've linked the buttons to call the same action when they're pressed, or else you wouldn't be needing to distinguish by tag, so your method should look like:
- (IBAction)buttonPressed:(id)sender
{
UIButton *aButton = (UIButton *)sender; // we know the sender is a UIButton object, so cast it
if (aButton.tag == 0)
{
// button 1 pressed
}
else if (aButton.tag = 1)
{
// button 2 pressed
}
}
Yeah you can use the tag to retrieve the UIButtons, and apply the same logic with UIVIews (have a look at this method remembering that UIButton inherits from UIView).
Specifically where do you have problems? Can you post some code/pseudo-code of yours?