I want to disable input pad on iphone when pressing 'done'. Any ideas about how to detect button press event?
Programatic Method
To register button press events you can either do the programmatic approach:
[button addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside]
Where buttonPressed: is a method in the class "self":
-(void)buttonPressed:(id)sender;
You can specify a pointer to any object instead of "self" of course, as long as the object has a buttonPressed: method.
Interface Builder
You can also use Interface Builder. Create a method that you want to execute:
-(IBAction)buttonPressed:(id)sender;
Then right click on the button in interface builder. You should see a list of actions. On the "touch up" action click on the circle button, and drag a connector to the object representing the class where you put the buttonPressed: method.
Disabling Input Pad
I'm not sure what you mean by this. Are you talking about dismissing a keyboard input? If so, you have to call the "resignFirstResponder:" method. Say, for example, the keyboard pops up because you are editing a text field. To dismiss the keyboard call the following method on the text field:
[textField resignFirstResponder];
Related
I know this is silly question but as a beginner at xcode, and i can't find the answer from any source, can i call numpad (numberic keyboard) from my action button? I just want my action button can call numpad like text field do. what code should i insert in my action button?
my code:
- (IBAction)setLoopBtn:(id)sender
{
}
Thank you again for sharing your knowledge to noob like me.
Create a UITextField with a frame that puts it off the visible screen. Assign the type of keyboard you want to display as the default for that field. When the button is tapped, send becomeFirstResponder to your off-screen field.
-(IBAction) ButtonClick
{
txtField.keyboardType = UIKeyboardTypeNumberPad;
[txtField becomeFirstResponder];
}
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
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];
Is it possible to press a button in iPhone SDK programmatically without the user touching it?
You can just do:
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
Do you want the user to see the button as pressed? Set the highlighted property to true and it will look like being pressed.
Now, if you just want to simulate the consequence of a button being pressed, just call the selector you assigned to the "Touch Up Inside" or "Touch Down Inside" events.
You could synthesize a touch on the button's coordinates, and set the appropriate event here's how
Overlay a second larger button that is transparent and assign the same selector it that you have on the other button.
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.