This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dismiss keyboard on touch anywhere outside UITextField
I am using UIScrollView in my app. I am putting few text fields and button. I want to hide my keyboard when they touch outside of the textbox. (I mean they will be taping on the uiscrollview). I have tried a lot of things... I use UIGestureRecognizer but its not working... help please
Here's the solution just tried and it works.. Hope helps
Add the following code to your viewDidLoad;
-(void)viewDidLoad {
//create a tapGesture which calls a removeKeyboard method
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(removeKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.yourScrollView addGestureRecognizer:tapGesture];
}
-(void)removeKeyboard {
[self.yourTextField resignFirstResponder];
}
Hope helps....
Try the following code in your view...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([yourTextView isFirstResponder] && [touch view] != yourTextView) {
[yourTextView resignFirstResponder];
}
}
Related
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];
}
}
}
}
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];
}
I'm trying to hide the keyboard after a touch anywhere else on the screen. The code I'm using is based on this answer here.
IBOutlet UITextView *myTextView;
And the method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
What I don't understand is how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
I also gave this code a try but that one was breaking my navigation buttons (even with the solution mentioned in the comments)
Objective C:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
Swift:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
This is the best way I have found and it is very simple.
how I should link my UITextField to the touchesBegan method. Which sent event do I need to use? Also, shouldn't the method be an IBAction, because right now I can't connect my UITextField to it.
Because you don't. You have to override this method on the view of which the text field is a subview.
What I do, is change the overall UIView class to UIControl.
This gives you a touchDown event you can link up to a method to resignFirstResponder.
The UIControl still gives you all the functionality of a UIView.
-(IBAction)backgroundTap:(id)sender
{
[text1 resignFirstResponder];
[text2 resignFirstResponder];
[textLogin resignFirstResponder];
[textPassword resignFirstResponder];
} // Resign all responders
- (void)viewDidLoad
{
//for keybord hide when touch outside:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(hidekeybord)];
[self.view addGestureRecognizer:tap];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)hidekeybord
{
[_yourtextfield.txt resignFirstResponder];
}
In .h
#property (nonatomic, assign) id currentResponder;
In .h
//in viewDidLoad:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(resignOnTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:singleTap];
//Implement the below delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentResponder = textField;
}
//Implement resignOnTap:
- (void)resignOnTap:(id)iSender {
[self.currentResponder resignFirstResponder];
}
Try the quick and dirty way:
Take a button and link it to an action method(lets call it Background). Stretch the button so it covers the whole view. Adjust the layers of the views so only things the user interacts by touch are on top of the button. Change the type of button to custom, this make the button invisible. Dismiss the firstResponder in the method Background.
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];