Hello I'm trying to have two of my UITextFields' keyboards hide when the user clicks off of the keyboard. I need both of my textfields to have the same method. How can I do this without duplicating them and giving Xcode and error?
Thanks!
Here's the method I'm using for my first textfield I need the same for my other.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([text1 isFirstResponder] && [touch view] != text1) {
[text1 resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
Apple strongly recommends to use gesture recognizers, not touchesBegan and other "touches" methods if you do not really have to use them.
Try this code. It will work for any numbers of text views inside view controller.
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onBackgroundTapped)];
[self.view addGestureRecognizer:tapRecognizer];
... another initialization code
}
- (void)onBackgroundTapped
{
[self.view endEditing:YES];
}
Related
This question already has answers here:
iOS - Dismiss keyboard when touching outside of UITextField
(38 answers)
Closed 6 years ago.
I am using this method but keyboard isn't hiding.
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_myTextView resignFirstResponder];
}
How can I dismiss the keyboard on simply touching outside the keyboard. Would be great if the solution is for both objective c and swift. Thanks
I have a query in objective C for textView inside scrollView. Please resolve. I've searched but have found no discrete solution.
try this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}
or use like
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextView class]]) {
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
the above code not work try this
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *resignView = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTapforResign:)];
resignView.cancelsTouchesInView = NO;
[self.view addGestureRecognizer: resignView];
}
Dismiss what ever is currently editing:
- (void) handleSingleTapforResign:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}
This question already has answers here:
Hide UITableView on touches outside the tableview
(3 answers)
Closed 6 years ago.
How to dismiss the tableview when we click on the view?i am using the touch began method.but its not working?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touches began");
UITouch *touch = [touches anyObject];
if(touch.view!=myTableView){
myTableview.hidden = YES;
}
}
how to disable the table when we click on view.i am having three tableviews?
Try this
ViewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handelGesture:)];
[self.view addGestureRecognizer:tap];
ActionMethod
- (void) handelGesture:(UITapGestureRecognizer*)sender
{
myTableview.hidden = YES;
}
Hope this helps.
Please check this code it working if you modified for your requirement Thanks
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *subviews = [self.view subviews];
for (id objects in subviews) {
if ([objects isKindOfClass:[UITextField class]]) {
UITextField *theTextField = objects;
if ([objects isFirstResponder]) {
[theTextField resignFirstResponder];
}
}
}
}
I have a UITextField on a UITableView and I am using a number keyboard however I want it to be dismissed when user clicks on anything but the UiTextField.
I have seen several solutions however there appears to not be one definitive answer. For example some talk about gestures and when I implement them they do not appear to work using the code below:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:TRUE];
}
As you can see I am trying but there is not one way that seems to be working. Can someone guide me please?
Thanks
Usually, you will want to hit-test the touch against areas which should not dismiss the keyboard; but in general the requirement is to tell the currently-in-focus control to "resign" it's status as "firstResponder". It might look like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.userInput resignFirstResponder];
}
However, you may also want to consider a special gesture recognizer for this, so that you are not having to over-analyze the NSSet of touches in the long run (delegate to the GestureRecognizer the task of determining the difference between an actual "dismiss pleas!" tap and a "can I scroll this?" swipe.
You should use resignFirstResponder instead :
[textField resignFirstResponder];
Use the following code
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
// Note the '!':
if(![[touch view] class] isKindOfClass [UITableViewController class]]){
// It's not a bubble they touched, dismiss the keyboard:
[self.view endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
or else
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
// Note the '!':
if(![[touch view] class] isKindOfClass [UITableViewController class]]){
// It's not a bubble they touched, dismiss the keyboard:
[textField resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
this helps in doing what you want
I'm making a tweak that involves popping an alert whenever the user double taps an icon in editing mode. I've tried hooking on to
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
of SBIcon and then
{
%orig;
UITouch *touch = [touches anyObject];
if (touch.tapCount == 2 && [[objc_getClass("SBIconController") sharedInstance] isEditing])
{
//pop an alert and do stuff
}
}
But this doesn't seem to work at all. Could anyone tell me what is wrong with the above and suggest alternative ways to achieve this?
EDIT: I'm using theos, if it matters.
I will suggest you to use Tap gesture recognizer.It is more specific and works very accurately.
For more info you can check out this link:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html
Alternative ways for you
If your icon is a button then you can easily detect the Double tab by adding UIControlEventTouchDownRepeat event
[yourIconButton addTarget:self action:#selector(multipleTap:withEvent:)
forControlEvents:UIControlEventTouchDownRepeat];
-(IBAction)multipleTap:(id)sender withEvent:(UIEvent*)event
{
UITouch* touch = [[event allTouches] anyObject];
if (touch.tapCount == 2) {
// Do all your actions here
}
}
If you are considering it for a whole view then use UITapGesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)];
[tap setNumberOfTapsRequired:2];
[yourIconView addGestureRecognizer:tap];
- (void)tapAction:(UIGestureRecognizer *)gestureRecognizer
{
//Do your action
}
I'm currently working on a project in iOS where I want the UItouch command to only send co-ordinates out when it's over a certain "image". I've got the co-ordinates outputting using UItouch but am not able to do this for just one specific area.
To my knowledge, the only way to do this is with views. So I've made a new view within my mainview, and from there I have problems and can't seem to get it working.
Has anyone done this before/can give me any advice on this?
PS - I'm using Xcode 4.
Thanks in advance.
Try this out
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (touch.view==yourImageView) {
//Coordinate code
}
}
EDIT: Or try using the UITapGestureRecognizer
In your interface add the UIGestureRecognizerDelegate
#interface ViewController : UIViewController <UIGestureRecognizerDelegate> {
then in your viewDidLoad add this
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMethod)];
tapped.delegate=self;
tapped.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapped];
then in your viewController add these 2 methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view==yourImageView) {
return YES;
}
return NO;
}
-(void)tapMethod {
//Coordinate code
}
And make sure you have [yourImageView setUserInteractionEnabled:YES];