How to pass multiple Selectors in UIButton - iphone

This is my code for a back button.
I have 9 buttons (button1,button2...button9)
And i want this button to activate if i press any of the other 9 buttons.
So is there a way where i can do this?
[_backButtonOutlet addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];

Create some method with will call all the desired methods that you want to call at the same time and call that method on button tapped event. Good Luck!

-(IBAction) button1:(id)sender
{
[self btn1Pressed:sender];
[self btn2Pressed:sender];
[self btn3Pressed:sender];
[self btn4Pressed:sender];
[self btn5Pressed:sender];
[self btn6Pressed:sender];
[self btn7Pressed:sender];
[self btn8Pressed:sender];
}

_backButtonOutlet is a reference to the button so you don't want it to be called when any buttons are pressed. button1: is the method that you want to be called when any button is pressed.
Option 1:
call:
[otherButtonOutlet_N addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
on each of the other buttons (replace otherButtonOutlet_N as appropriate).
Option 2:
In your XIB / storyboard, connect all of the buttons to the button1: method. You can add multiple target/action pairs to a button.

Related

Change Action of a button depending on a Pickerview Choice iphone sdk

I explain you what I'm trying to do.
I have a PickerView, and a button, i'd like when I select the row "Action1" on my Pickerview, the action become a UIAlert when i click on my button., and when I select the row 'Action2', the action become a ModalTransition when i click on my button.
How I can do it ?
Thanks
set a method for button click as
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
in method you can implement a case where the case options can be the values from the picker output(set a integer to know the selected index of picker via UIPickerViewDelegates (pickeroption)
-(void)method1
{
switch pickeroption
case 1:
Action1
break;
case2:
action 2
break;
}
There could be multiple solution to do this
Set Button Tag as per PickerView selection and in Action/Selector Method you can create Switch Case based on Tag. And can perform Action
You can directly make switch case on Picker View in Action/ Selector Method.
But if you want to set Multiple selector on different state of button you can use
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchDown];
Hope this will help you out.

Calling to function from UIBarButtonItem

I have button in my view with a setAction to a local function.
in GameOptions.m i have the next function:
- (void) changeSettings (UIBarButtonItem *) sender {
// do something
}
and a button that start it in GameOptions.m :
[btn1 addTarget:self action:#selector(changeSettings:) forControlEvents:UIControlEventTouchUpInside];
now i moved it from his view to a navigation controller as an rightBrButtonItem.
the myVC is an instance of small UIViewController with all the navigationBar settings,and each time i using pop ,push to it .
UIBarButtonItem *buttonNav1 = [[UIBarButtonItem alloc] initWithCustomView:settingsView];
[myVC.navigationItem setRightBarButtonItem:buttonNav1];
the problem is that this button is not in his GameOptions class and i want it to start the same function with implementation in GameOptions.m.
I would like to do the next thing :
buttonNav1 setAction:changeSettings;
but its impossible, the changeSetting is not recognized...
Tried : buttonNav1.action = self. but i'm not getting this function, only functions without parameters.
How can i call to chnageSettings from that button in navigationBar
All you need to do is inform your UIBarButtonItem of the existence of the GameOptions object.
That's what you were doing using self in :
[btn1 addTarget:self action:#selector(changeSettings:) forControlEvents:UIControlEventTouchUpInside];
Just after instanciating your UIBarButtonItem *buttonNav1, you can send it the message addTarget:action:forControlEvents: or, if you want to use setAction, don't forget to also use setTarget.
So now, your question is: what is the most elegant/efficient way to pass the GameOptions object's pointer to the UIBarButtonItem ;)

Objective C Syntax: addTarget on UISwipeGestureRecognizer

I'm currently working on an app where the user has the option to either swipe through data, or use a button to go through the data. I'm having trouble understanding how to combine two bits of code.
Here is the code I'm using for swiping:
- (void)swipeRight:(UISwipeGestureRecognizer*)recognizer
{
if ([questions hasPrevQuestion] == YES) {
[self vorige:nil];
}
}
(the [self vorige:nil]; is calling the method for the button, so the swiping and the button have the same behavior)
and I need to somehow incorporate this code which applies to the button:
-(void)animationDidEndOnAnswer {
[vorigeButton addTarget:self action:#selector(newQuestion:) forControlEvents:UIControlEventTouchUpInside];
}
I think it's pretty simple, but I just cannot for the life of me figure out how to call the swiping method place of the button here...I'm thinking it's simple because I found this example in the UIGestureRecognizer class reference:
- (void)addTarget:(id)target action:(SEL)action
but being new to objective-c, I don't really know what to do with this. any help is very appreciated.
- (void)addTarget:(id)target action:(SEL)action is the code equivalent of binding a button action to a method like you do when you ctrl-drag from a button to an IBAction in Interface Builder.
So if your method is called vorige: and you want it to be called when the button is tapped, you would say:
[vorigeButton addTarget:self action:#selector(vorige:) forControlEvents:UIControlEventTouchUpInside];
But I don't know why you would want to do that as a result of an animation - you normally would set the button action once when the view is loaded, not change it during an animation.
Nick's solution is good.
if you want to call the same method for the swiping & the tap on your button, you can tweak your swipeRight like this:
- (void)goThrougtData:(id)sender
{
if( [sender isKindOfClass:[UISwipeGestureRecognizer class]] ) {
// swipe specific code
}
else if( [sender isKindOfClass:[UIButton class]] ) {
// tap specific code
}
if ([questions hasPrevQuestion] == YES) {
[self vorige:nil];
}
}
and in your init method, you add
[mySwipeRecognizer addTarget:self action:#selector(vorige:)];
[vorigeButton addTarget:self action:#selector(vorige:) forControlEvents:UIControlEventTouchUpInside];

How to make selected button highlighted?

I have four buttons. I want that selected button keep highlighted till other button is selected. After that other button gets highlighted and first button unhighlighted.
- (void)doHighlight:(UIButton*)b {
[b setHighlighted:YES];
}
-(IBAction)sizeBtnClicked:(UIButton*)btn{
[self performSelector:#selector(doHighlight:) withObject:btn afterDelay:0];
}
My problem is after selecting other button first button does not unhighlight.please guide me how to do it.
declare a variable button tempBtn in your .h , alloc it and then perform this.
-(IBAction)sizeBtnClicked:(UIButton*)btn{
[tempBtn setHighlighted:NO];
[btn setHighlighted:YES];
tempBtn=btn ;
[self performSelector:#selector(doHighlight:) withObject:btn afterDelay:0];
}
You simply get the reference of last selected button
and then in the sizeBtnClicked, set the highlighted property of last selected button to NO.
In nib file you can change a image for button selection in inspector

cocoa UIImageView addTarget with extra parameter

With a loop I add UIImageView to a UIScrollView i need to add an extra parameter addTarget so when i click i can log the index.
[imageButton addTarget:self action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
-(IBaction) buttonPushed: (int) index
{
NSLog(#"%d",index);
}
How do i achieve this?
When you add a target, the method being call can either have no arguments (e.g. buttonPushed) or have one (buttonPushed:) which is the control sending the event (in this case your button). If you want an index, or any other value, you need to set it on the button sending the event. For example, when you setup the buttons:
myButtons = [NSArray arrayWithObjects:myFirstButton, mySecondButton, nil];
[myFirstButton addTarget:self action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
[mySecondButton addTarget:self action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
and implement your action as
- (IBaction)buttonPushed:(UIButton *)button
{
NSLog(#"%d",[myButtons indexOfObject:button]);
}
Use the tag property of the button