iphone development: one button two actions - iphone

In my application I have a button and when it is pressed, lets say "touch Up inside" another view is opened. well my question is, how can I do two actions? I mean, when I pressing the button I want something to do(like hiding the button or changing the image of the button), and when I stop pressing I want to be navigated to the another view.

Do this:
[yourbutton addTarget:self action:#selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
[yourbutton addTarget:self action:#selector(touchDown:) forControlEvents:UIControlEventTouchDown];
Now selectors are:
-(IBAction)touchUp :(id)sender{
UIButton *btn = (UIButton *)sender;
[btn setImage:yourImage forState:UIControlStateHighlighted];
}
-(IBAction)touchDown :(id)sender{
//Navigate here
}

If you want to 'do something' when 'pressing', then just hook up 'Touch Down'

The class that is the delegate for the button needs to track the state, and manage that state through the events that can occur (a state machine). Use instance variables to track the state.
When you handle the button action, the code should determine what state it is in, and make any changes necessary to move to the next state.

You can use different actions for different ControlEvents
[button addTarget:self action:#selector(action1) forControlEvents:UIControlEventTouchDown] ;
[button addTarget:self action:#selector(action2) forControlEvents:UIControlEventTouchUpInside] ;

Just add in your code to IBActions. Then like the actions through the interface builder to different touch events.
Code:
[yourUIButton addTarget:self action:#selector(touchUp:) forControlEvents:UIControlEventXXXXX];
There are a lot of events you can link your UIButton to:
UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
Ref:UIControl Apple Reference

Related

Change UIButton image when button pressed

I'm trying to change the image button just in the moment that the button is pressed. When released, i want go back to the original.
I read some posts here, but it's not working.
Is it possible do it in the GUI without programming?
I tried this:
-(IBAction) toggleUIButtonImage:(id)sender{
if ([sender isSelected]) {
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
But what kind of sender events should i associated the function toggleUIButtonImage ?
Thanks
You can do it in interface builder by giving different settings to each button state config:
Or you can also change the 'settings' of the button in code by using 'UIButton' setImage:forState method.
[myButton setImage:highlightedImage forState:UIControlStateHighlighted];
Usually, you should do it by 'configuring' the button as described above and not by changing the sub views of the button manually as a response to user interaction. The reason it doesn't work for you is the inherent hidden behaviour of UIButton. You manually make the change of a subview of the button as a response to user interaction, but your change is changed back to what was defined in the settings of your button (according to the button's state). So it seems like your code doesn't do anything at all.
in the ViewDidLoad Method
set the property of
[[button setImage:[UIImage ImageNamed:#"unselectedImage.png"] forState: UIControlStateNormal];
[[button setImage:[UIImage ImageNamed:#"selectedImage.png"] forState: UIControlStateHighlighted ];
This can be done with Interface Builder, no code necessary.
Select your button in IB, go to the right sidebar, change the Type to Custom, make sure State Config is Default, and type in the name of your "regular image" reference in the Background Image text field.
Then change State Config to Highlighted, and type in the name of your "pressed image" reference in the Background Image text field.
In IB, you can set the image for the Highlighted (not Selected) state of the button. That should do it.
Look in the Button section of the Utilities pane, and select Highlighted from the State Config drop-down.

How to enable touchesMoved() with button action?

I am doing one piano application. There i am using button action to play music. But its not supporting drag functionalities. I have used touchesMoved(),touchBegan() methods also . But nothigh is working.Can anyone help me, please?
Button supports drag functionality. You should add your target by below control events.
[button addTarget:target action:#selector(action1) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:target action:#selector(action2) forControlEvents:UIControlEventTouchDragOutside];
[button addTarget:target action:#selector(action3) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:target action:#selector(action4) forControlEvents:UIControlEventTouchDragEnter];
Implement proper actions for each event and it should identify proper drag event.

How to remove the greyed out look of a disbled UIButton

I've got a UIButton that i want to look exactly the same when it's in its disabled state as when it's in its Normal state. Right now it has a slight greyed out look to it.
Do not use enabled property or setEnabled:NO method, instead use:
[myButton setUserInteractionEnabled:NO];
That would prevent the button for being touched, but without changing his looks!
The other way is if your button is a custom button and has an image:
[button setImage:someImage forState:UIControlStateNormal];
[button setImage:someImage forState:UIControlStateDisabled];
[button setEnabled:NO];

How to determine which event called imageMoved:withEvent:forControlEvents?

I've added some targets to a button. They work fine. But in my imageMoved... method, how do I determine which one of the two events fired it? Here is where I added the targets:
[button addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragExit];
I saw an example recently where the event is tested in the imageMoved method, something like:
if (event == UIControlEventTouchDragExit )
but that won't compile. I'm not finding any examples online nor any clues in UIControl Class Reference.
What I'm trying to do with this is store the original location of the button/image so that if the user doesn't complete the drag to a target it will snap back to where it came from.
Thanks for any help anyone can offer.
Use two different methods
[button addTarget:self action:#selector(imageMoved:withDragInsideEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(imageMoved:withDragExitEvent:) forControlEvents:UIControlEventTouchDragExit];
and reuse common functionality in 3rd method.

how can i fire button action event without any user action iphone xcode?

how can i fire button action event without any user action?
pls give me any sample..
UIButtons are subclass of UIControl. They use the sendActionsForControlEvents: method to send out events. You can call that method on the button (or control) to have the actions sent.
[theButton sendActionsForControlEvents: UIControlEventTouchUpInside];
If user dont want to fire action on button,why do you show button,without showing button,u can do action.I think u need dynamic code (without interface builder),the code is
[button1 addTarget:self action:#selector(action1:) forControlEvents:UIControlEventTouchUpInside];
-(void) action1:(id)sender;
{
}
hope this will help....