UIButton Interchange with touch animation in IPhone? - 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);
}

Related

Control more than one button using tags

I have 28 buttons in my application. I need to control them in a single function. All I want to do is make all the buttons visible. I gave tags to the buttons. I tried it with a for loop but I couldn't do it how can I solve this problem?
(IBAction)btnAction:(id)sender{
UIButton *btnPressed = (UIButton *)sender;
NSUInteger i=btnPressed.tag;
for(i=0; i<29; i++)
{
btnPressed.hidden=NO;
}
}
Instead of tags, use an IBOutletCollection. So now you have a single NSArray pointing to all of the buttons. Now just cycle through that NSArray.
You can create a single IBAction method, check tag values and then do what you want to do
- (IBAction)btnAction:(id)sender{
UIButton *btnPressed = (UIBUtton *)sender;
// Check button tags and write code accordingly //
}
use IBOutletCollection. see the following link for your answer.
IBOutletCollection
and
IBOutletCollection (UIbutton)
IBOutletCollection of UIButtons - changing selected state of buttons

iOS Determine button that was programmatically created

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 ....
}

how to pass parameter when different button call same class?

I am developing for iPhone the app I am developing has many buttons and I want all buttons to call the same class but with different parameter
for example I want button1 to call the displayimageclass
and button2 should call the same class displayimageclass so, how to different method call same class and in that same class how to fetch different button from where it is call to particular class.
That is pretty much standard. Just define as many IBAction methods as you need.
In the viewController's .h file:
- (IBAction)actionButton1:(UIButton *)sender;
- (IBAction)actionButton2:(UIButton *)sender;
- (IBAction)actionButton3:(UIButton *)sender;
And in its .m file:
{
//react here to button1 pressed
}
- (IBAction)actionButton1:(UIButton *)sender
{
//react here to button1 pressed
}
- (IBAction)actionButton2:(UIButton *)sender
{
//react here to button2 pressed
}
- (IBAction)actionButton3:(UIButton *)sender
{
//react here to button3 pressed
}
Then associate the action with the Button's events (TouchUpInside would be most appropriate, I guess).
You should add tag property to your buttons.
Suppose your buttons are
button1.tag =1;
button2.tag =2;
button3.tag =3;
When you click any button, NSLog shows related button tag number. i.e You will get your result what you want.
- (IBAction)buttonClicked:(id)sender {
UIButton* button = (UIButton *)sender;
NSLog(#"The number of tag:%d",button.tag);
}
I think it will be helpful to you.
you can implement your action method like this:
- (IBAction)action:(UIButton *)sender
{
//sender will be different for different button
}
Set tag value for buttons and get those buttons like
- (IBAction)action:(UIButton *)sender
{
UIButton *btn = (UIButton*)sender;
}
Are you looking for
1) several buttons performing the same method with the same behavior
2) several buttons performing the same method but having different behavior based on the button characteristics
3) each button performing its own method.
1) You can register several buttons on the same IBAction, they all will call it and perform the same code
2) In the method were all the buttons are registered (cf: 1) it is possible to sort the buttons by subclassing the UIButton class, giving an enum value that represents what the button should perform and then in the called method check the enum value with a switch in order to trigger the wanted behavior
The UIBarButtonItem already has this tag property so you could use them by giving a value to each of your UIBarButtonItem in the Interface Builder and set them to call the following method
- (IBAction) myButtonsMethod:(id)object
if ([object isKindOfClass:[UIBarButtonItem class]])
{
switch ([(UIBarButtonItem *)object tag])
{
case myFirstTagValue
...
}
3) #Hermann Klecker solution is the right one.

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?