2

In my app I have something like this:

- (IBAction)backgroundTouch:(id)sender 
{
    [businessDescription resignFirstResponder];
    [self.view endEditing:YES]; 
}

I am not sure which of the two lines I use is better so I use both :) It works when the text area is highlighted, and the user presses the background.

But users do not always press the background, and sometimes press on other page elements like the next element they are trying to fill out.

In my screen shot, I have the next element below the text area, and when I click there, the keyboard doesn't get hidden. Could anyone help me hide the keyboard when the user clicks on various page elements, and when the textarea happens not to be highlighted?

enter image description here

Here is my .h file:

@interface PlanBusinessController : UIViewController

@property (weak, nonatomic) IBOutlet UITextView *businessDescription;

- (IBAction)submitBusiness:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *buttonProperty;

@property (weak, nonatomic) IBOutlet UITextField *personName;

@property (weak, nonatomic) IBOutlet UITextField *personEmail;

@property (weak, nonatomic) 
    IBOutlet UISwitch *privacy;

@property (weak, nonatomic) IBOutlet UISwitch *wantHelp;

- (IBAction)helpToggle:(id)sender;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;

@property (weak, nonatomic) IBOutlet 
UIButton *test;

@end

and here are my .m declarations:

#import "PlanBusinessController.h"

@interface PlanBusinessController ()

@end

@implementation PlanBusinessController
@synthesize nameLabel;
@synthesize emailLabel;
@synthesize businessDescription;
@synthesize buttonProperty;
@synthesize personName;
@synthesize personEmail;
@synthesize privacy;
@synthesize wantHelp;
@synthesize test;


-(void)touchesBegan:(NSSet*)touches
{
    UITouch *touch=[touches anyObject];
    UIView *view=touch.view;
    if (![view isEqual:businessDescription])
    {
        //[businessDescription resignFirstReponder];
    }
}

- (IBAction)backgroundTouch:(id)sender 
{
    [businessDescription resignFirstResponder];
    [self.view endEditing:YES]; 
}

Thanks!

GeekedOut
  • 16,905
  • 37
  • 107
  • 185

3 Answers3

4

I use this method in my program ViewController and it works fine. I'd give it a try.

//Used with the text fields to dismiss keyboard
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [textField resignFirstResponder];
}

If you have other elements, then in the methods for those elements also add [textField resignFirstResponder].

For example, if they can click on a button, write something like this:

-(IBAction)button1:(id)sender
{   
    [textField resignFirstResponder];

     //Do stuff
}

Note: You need one for each textfield you want to close. For example:

-(IBAction)button1:(id)sender
{   
    [textField resignFirstResponder];
    [textField2 resignFirstResponder];
    []... etc

     //Do stuff
}
MrHappyAsthma
  • 6,332
  • 9
  • 48
  • 78
  • thanks, so basically make something like that for every element on the page like this? -(IBAction)some_random_page_element:(id)sender – GeekedOut Jul 25 '12 at 18:26
  • Exactly! The first one with the touches is for background elements. You have to add in an "IBAction" for every element such that the first thing it does is close the keyboard by "resignFirstResponder". – MrHappyAsthma Jul 25 '12 at 18:28
  • I tried doing something like this -(IBAction)privacy:(id)sender { NSLog(@"privacy clicked"); } just to see if it responds to when I click on it, but it seems to not be getting called. Would you know why? It is one of my synthesize parameters – GeekedOut Jul 25 '12 at 18:50
  • Did you open the Interface Builder (IB) and link it to the object you want to have that action? – MrHappyAsthma Jul 25 '12 at 18:51
  • when you say the InterfaceBuilder, is that the same thing as the storyboard? I did not link every individual element. – GeekedOut Jul 25 '12 at 18:52
  • I'm not sure what you mean by storyboard. I only use xcode 4.2, but the Interface Builder is the application that opens when you click on your ".xib" or ".nib" file. – MrHappyAsthma Jul 25 '12 at 18:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14422/discussion-between-mrhappyasthma-and-geekedout) – MrHappyAsthma Jul 25 '12 at 18:54
2

Please check the below code:

-(void)touchesBegan:(NSSet*)touches
 {
    UITouch *touch = [touches anyObject];
    UIView *textView=touch.view;
    if (![textView isKindOfClass:[UITextView class]])
    {
        [businessDescription resignFirstResponder];
    }
 }

When you touch an object, it will check that, touched object is a type of UITextView or not, if not it will end editing.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
1

An easy way would be to catch the view on which the touch has occurred. And compare it against the text area. If the view or (something else like button) is different from the text area then you can hide the keyboard. Here's pseudo code:

 -(void)touchesBegan:(NSSet*)touches
 {
    UITouch *touch=[touches anyObject];
    UIView *view=touch.view;
       if (![view isEqual:textArea])
        [textarea resignFirstReponder];
 }
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
  • this looks like mostly real code and not pseudocode :) I am a bit confused with the last line. I am getting a syntax error that there is no visible interface for UITextView on the variable I use for the textArea – GeekedOut Jul 25 '12 at 18:31
  • Did you add a UITextView to the .h file and synthesize it? It will also need attached in the IB. In your case, replace "textArea" with businessDescription. – MrHappyAsthma Jul 25 '12 at 18:34
  • @MrHappyAsthma I do get this error when I run the app on the simulator: Unknown class PlanBusiness in Interface Builder file. ....I will post my .h and .m declarations in the original question. – GeekedOut Jul 25 '12 at 18:36
  • If you go to Build Phases within your targets you will see Compile Sources. Make sure each of the .m files you need are listed. If not, just add it here and the warning should go away. Try that. – MrHappyAsthma Jul 25 '12 at 18:45
  • @MrHappyAsthma sorry I am a bit of a newbie in ios :) which one is BuildPhases and how to do get to it? :) – GeekedOut Jul 25 '12 at 18:47
  • @MrHappyAsthma I am using the latest, its 4.3.3 – GeekedOut Jul 25 '12 at 18:50
  • You click on the blue project file and then go down to "targets". Then select Build Phases and go to the Compile Sources tab. Make sure every .m file you use is listed there. – MrHappyAsthma Jul 25 '12 at 18:52