Control more than one button using tags - iphone

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

Related

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 point to an object from string

My question is kind of general; in my project I have 10 UIButton objects
named Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, and Button10
I also have UIImageview That are named exactly like the buttons from 1 to 10.
I want to write a code that will manipulate the image by the last character of the button (always a number from 1 to 10) and will affect the UIImageview the same way
Something like this
buttonlastcharacter = i;
if(sender.lastcharacternumber is:i){
Button%,i.frame = //Some manipulation
But basically all that I want is to have access to a certain object by string
How can I implement such a behavior?
There are a couple of better ways to do this. If these buttons are all static and in IB you can use an IBCollection array for image views and buttons to simply call them up by matching indexes.
Better yet just use the tag value for the buttons or image views.
It is maybe not the ideal solution in your case, but you can do it different ways:
using kvo
UIButton* myButton = [self valueForKey:[NSString stringWithFormat:#"button%i",i]];
or with selectors and properties
UIButton* myButton = [self performSelector:NSSelectorFromString([NSString stringWithFormat:#"button%i",i])];
Hm, you could use an array for your buttons and a tag for your UIImageView objects. They all inherit from UIView, which provides you with a .tag propterty. It is of type NSInteger* .
For convenience reasons I would suggest to name the buttons from 0 to 9. It does not really matter but the first index in the array would be 0 and therefore naming them accordingly just makes things easier.
Define
NSArray *buttonArray;
You may opt for NSMutableArray depending what else you may want to do with it.
In viewDidLoad code:
buttonArray = [NSArray arrayWithCapacity:10];
buttonArray[0] = button0;
..
buttonArray[9] = button9;
In your XIB file in Interface Builder, or whereever you may create the UIImages programmatically, add the tags accordingly.
image0.tag = 0;
...
image0.tag = 9;
assuming you name them image0 to image9.
In your appropriate action method code:
buttonArray[sender.tag] = someManipulation;
You can do it like this,
In your IBAction method:
- (IBAction)click:(id)sender
{
UIButton *but = (UIButton *)sender;
but.frame = your manipulation code;
}
or you can check the title like:
if([but.currentTitle isEqualToString:#"Button1])
{
//Manipulate button 1
}
if you have added tags for the buttons from 1-10 you can use,
if(but.tag == 2)
{
//Manipulate button 2
}

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?