pressing event in UIButton? - iphone

when i am pressing my UIButton ,(i did not release my mouse button),i want to change
title? i tried touchupinside through XIB.but it was called after i released my mouse button.
i tried both land also all states....?
[sButton setTitle:#"hai" forState:UIControlStateNormal];
[sButton setTitle:#"hai" forState:UIControlStateHighlighted];

The Control Events documentation will list all of the events you can subscribe to for controls (in this case, your UIButton). The one you want is UIControlEventTouchDown. But the problem with your requirement is that pushing the button covers the screen with the user's finger and I wonder whether changing the title will really be beneficial.

Related

Show hint on touching the button

I want to display some hint to user when a user touches the button in iPhone native application, just like tooltip in web applications.
I have a large number of TextField that should be associated with info button to let the user know the detail description about the data captured in that TextField.
Can any one post steps to achieve this?
Agreed with Justin, but if you really had to:
Add an action to the UIButton that launches on the Touch Down event, e.g. UIButton * button = [[UIButton alloc] init]; [button addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchDown];
Define the 'doSomething' action method as showing a custom 'tooltip' view anchored to the button location. The UIButton which was touched will be passed to the action method, from which you can access its location and so know where to anchor the tooltip.
If this all sounds laborious, check out "Is it possible to show a tooltip in an iOS app?", which links to CMPopTipView (https://github.com/chrismiles/CMPopTipView#readme), a useful package which will do it all for you. Haven't used it myself, though.
I'm not sure how to achieve this, but it sounds like a terrible User Interface idea for a mobile app.
I say that because tooltips in classic applications typically happen on mouse hover. Mobile interfaces don't have an equivalent gesture. When you touch a button, it's like a click.
If you look at most mobile applications that need to explain something to the user, it's usually done through visual elements that show up when the app loads (they'll point to different buttons, explain what they're for, and then go away after the first time the button is used).

how to drag and drop UIButton

I want to drag and drop UIbutton in my app like iPhone main screen icons.
I can do this but while clicking the button it should perform an action. But this action is also called while dragging the button. I am using UIControlEventTouchDown for touch action and UIControlEventTouchDragInside for dragging the button.
Maybe you can use UIControlEventTouchUpInside for this action instead touch down.
In UIControlEventTouchDragInside you may change the shared variable named like "dragPerformed" to YES, and then, if it is NO perform click button action in UIControlEventTouchUpInside, if YES - do nothing (that is drag) and turn it to NO

How to open a view with a button clicked as default

In my app i want to open a view with the content of a particular button (so that button should look clicked and should be not clickable). I have 4 button with pictures and all the four have different content inside them (Table view with different content).When this view gets open i want the first button clicked automatically and the content of that button should get displayed and by clicking any other button the content of that button should get displayed and the previous clicked button should be available to click again.
I am using different pictures for clicked and unclicked button.
Thanks,
Maybe this will help you
- (void)didClickButton:(id)sender {
UIButton *optionButton = (UIButton *)sender;
if(lastSelectedButton.tag!= optionButton.tag) {
optionButton.selected = YES;
//According to your needs enable or disable the button's interaction
}
Here lastSelectedButton should be an instance variable.
What you're describing sounds like a segmented control. Essentially the segmented control works like buttons on a tape recorder (dating myself, I know.) When you press Play, it stays down and can't be pressed again until you press Stop or FF or Rew, etc. (Ok, Stop doesn't really work that way, but the rest of the buttons do)
Unfortunately, I don't believe you can use your own images in a UISegmentedControl, but fortunately there's an open-source version that should work for you: https://github.com/xhan/PlutoLand/blob/master/PLSegmentView.h
Once you have the control in place you can change the content of your main view depending on the value of the segmented control. You can handle that in the UIControlEventValueChanged event
Keep a single selector for all the buttons something like
[btn addTarget:self action:#selector(templateSelected:) forControlEvents:UIControlEventTouchUpInside];
and make use of the tag to carry any index to the selector
[btn setTag:integer];
and if you want to keep track of previously clicked button then keep a global (id) and assign the current button address to the that id.
And if you want the first button to be clicked on load then call the function melodramatically during initialization of the first button.
[self templateSelected:firstButton];

How to make UIButton respond only to touch, not drag

Take a look at this method:
[textButton addTarget:self action:#selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchDown];
When the button is touched, it calls articleModalWillAppear:. That works well. The problem is the button also calls the action when it is dragged. (The button is big and contains a text paragraph)
I put six such buttons as subviews of UIScrollView (with UIPageControl). It works well when dragging UIScrollView horizontally. But it pops up modal views when dragging vertically because when a finger dragging across a button, the button considers it as a touch, and the touch calls articleModalWillAppear:.
If you still don't understand my problem, think about the New York Times iPad app. You can drag the pages horizontally. You can touch an article description to go to the full article view. But nothing happens when you drag vertically inside an article description. How to achieve this?
Use a different event e.g.:
[textButton addTarget:self action:#selector(articleModalWillAppear:) forControlEvents:UIControlEventTouchUpInside];
UIControlEventTouchUpInside is the most generally used in this case.
use forControlEvents:UIControlEventTouchUpInside
That only registers a click event when a user touches down inside the button and then touches up in the same button, and ignores drags.

UIButton delayed state change

I have a UIButton subview inside of a UITableViewCell.
When this button is touched, the user must hold the button for about a half second for the button's image to change to the UIControlStateHighlighted image.
This means that if the user just taps the button as is usually the case, the highlighted state is never shown.
Why does this occur and how can I fix it?
I just encountered this problem and saw that this issue hadn't been closed. After screwing around for a while I found a fix for it.
Now you can fix this by turning off delaysContentTouches or unchecking the "Delays content touches" box on the tableview.
The only negative side effect is that the user won't be able to tap down on a button and initiate a scrolling gesture. However, if the user tries to scroll starting from anywhere that doesn't itself accept touches, the behavior should be the same as before.
The problem is that your UIButton is inside a UITableView. This means that the table view has to determine whether your tap is going to be a swipe or if it's just a tap intended for the button. The table view has to delay sending a message to the UIButton until it knows that the user doesn't intend to swipe and therefore scroll the view instead of pressing the button.
If you don't need a table view, get rid of the UITableView.
Up for David Hodge's answer.
I just want to add a way to remove that "only negative side effect", already described by David: if you start scrolling inside a UIcontrol in a UIScrollView with delayContentTouches=NO, scrolling doesn't work.
SOLUTION
Subclass UIScrollView (or UITableView as the original question) and override:
-(BOOL) touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
Your UIControls inside UIScrollView/UITableView will change their state immediately on tap and the scrollviews will be able to scroll even if the touch starts on some UIControl. Works like a charm.
I just change the image from within the target action method:
[sender setBackgroundImage:[UIImage imageNamed:#"highlighted-image.png"] forState:UIControlStateNormal];
It changes the background image instantly.
Edit: completely re-written following a misunderstanding of the question
One way of thinking of a UIButton is as a shorthand way of setting up an area of the screen that can respond to various instantaneous touch events the response it makes is defined by UIControl's Target-Action system for delivering messages to other objects.
UIControlEventTouchDown sounds like the one you need to respond to. It will be triggered as soon as someone touches inside your button - this is what the "Contact Info" button in SMS does.
UIButton* myButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// SEt up title, frame etc
[myButton addTarget:self action:#selector(myButtonWasPressed) forControlEvents: UIControlEventTouchDown];
[myMainView addSubView:myButton];
Will send a -(void)myButtonWasPressed message to the object this code runs from (ideally you view controller). In myButtonWasPressed you can then add a new view or take any action you like. The SMS app pushes a view controller to display the contact info using a navigation controller.
If this still doesn't solve your problem, you're going to have to post some code in order to get more insight into what's going wrong.