Event fired two times when clicked to button in my iOS. app - ios

I made a simple IOS. application where the UI contains buttons. For example I have nine buttons each button represent a number same as numeric keyboard. I made this buttons the following way, put it to the storyboard and made the Touch Up Inside event with ctrl drag functions, after I copied the buttons eight times.
The problem is when I pressed a button the event comes sometimes twice. It happens randomly. I put a break point into line NSLog I did not see faulty thing at all.
- (IBAction)digitPressed:(UIButton *)sender {
if ([lastPressed isEqualToString:[sender currentTitle]]) {
NSLog(#"Douple pressed digit"); // break it here
}
// store to lastPressed
lastPressed = [sender currentTitle];
}

If you get multiple times in digitPressed when clicking once I would guess something went wrong when you copied the buttons so check the wiring in your storyboard.
And as Vince said it's a good practice to use id, it's not because this method gets triggered by buttons now that they should always be triggered by a button.
It's easier to debug when you log every time instead of only doubles.
NSLog(#"Button pressed: %#", [sender currentTitle]);

Related

How to reset ViewController on the press of a button in iOS

My app has a ViewController consisting multiple textfield and custom UI elements arranged as a form with "submit" and "reset" buttons.
I want to reset all the textfields and custom UI elements when user clicks the submit button so that user gets a feeling that same Form is opened again.
I tried calling ViewDidLoad() and setNeedsDisplay() on the click of "Submit" button but data previously filled by user remains as it is.
Kindly Help!
Just set the text of all textfields to an empty string (the text value). There is no magic function to clean everything in a view.
The only other solution would be to actually display the ViewController again, but this is probably not what you want, because it causes overhead as well as you might see something of the switching on the screen.
If you programmatically coded your textfields and some other objects, you can refresh always your viewController everytime by adding all your UI codes in the predefined method: -(void)viewWillAppear:(BOOL)animated{} and you should remove all self.view elements by adding this code.
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
You codes in the viewWillAppear will look like this.
-(void)viewWillAppear:(BOOL)animated{
[self.view.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Codes for User Interface here
}

Trying to make my image's position change randomly

I am using Xcode to try to change my images position randomly every time a button is pushed but for some reason, the image only moves once in a while when the button is pushed even though I know it received the button's IBAction because the NSLog in the IBAction displayed in the console, I have tried it two ways and both methods only worked sporadically
Method 1:
ranX = arc4random() %320;
ranY = arc4random() %480;
myImage.center = CGPointMake(ranX, ranY);
Method 2:
myImage.center = CGPointMake(arc4random() %320, arc4random() %480);
One more thing: every time it does work, the next IBAction to run puts it right back into its original position.
More information that might be pertinent I don't know, I do not have size classes enabled, it is triggered by touch up inside. I also DID make all the RNGs arc4random_uniform.
The problem is fixed, just deselect auto layout.
What control event did you add to your button? Since you stated it moves every once in a while, you might have done the wrong event. Try UIControlEventTouchUpInside
So,
[myButton addTarget:self action:#selector(moveImage) forControlEvents:UIControlEventTouchUpInside];
You're using a storyboard I see, Make sure that the send event for the button is "Touch Up Inside". Click on the button, go to the Connections Inspector and double check this.

How would you program a Continuous Delete Button?

I'm making a calculator app and am supplying my own keypad with UIButtons. I have a delete key and everything works except that the user has to keep pressing the delete key over and over again if they want to delete all.
I was wondering if there is a way to delete everything when the button is held for more than 2 seconds.
The simplest way of implementing this would be attaching a long press gesture recognizer to your [Delete] button.
Xcode lets you attach long press gesture recognizer in the interface builder. Add it to your button, configure the duration of long press, and connect the handler to IBOutlet in the same way that you connect other UI events.
If you would rather do it in code, this answer shows you how.
Use your own timer function to handle this
-(IBAction)buttonHit {
//here start timer that fires for every 2 seconds and handle deletion method in that
}
-(IBAction)buttonReleased {
//Stop timer...
}
In your subclassed UIButton, you might want to watch the "touchesBegan: withEvent:" UIResponder method and if it passes a certain threshold of time, then start deleting like crazy (that is, until the "touchesEnded: withEvent" method gets called).

iOS: UIButton touch up inside doesn't respond multiple press

I have a UIButton whose IBAction has been setup though XIB file. Following is the IBAction method. When I press button multiple times very quickly, this method gets called only once for the last press. I have to wait little more time between the presses. Is there anything like long press time for UIButton that I can reduce or any other settings to make it responding quicker. This button does the similar job of Keyboard's back button to delete last character. I want it to respond very quick like Keyboard's button does. Thanks.
- (IBAction)deleteButtonPress:(id)sender {
NSLog(#"Click");
if(self.numpadTextFiled.text.length > 0)
self.numpadTextFiled.text = [self.numpadTextFiled.text substringToIndex: [self.numpadTextFiled.text length]-1];
}
Is the button by any chance positioned within a UIScrollView? If so, then touches are delayed?

How do I make the keyboard go away when the user clicks somewhere else? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dismiss keyboard by touching background of UITableView
How do I make the keyboard go away when the user clicks somewhere else?
Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.
I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.
So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.
Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?
There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.
You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,
-(void) hideKeyboard {
[textFieldName resignFirstResponder];
[textFieldSurname resignFirstResponder];
for (UITextField * txtField in arrTextFields) {
[txtField resignFirstResponder];
}
}
Then, at various sections in your class, depending on the functionality required, call;
[self hideKeyBoard];
This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.
How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.
Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

Resources