Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8182925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:00:25+00:00 2026-06-07T01:00:25+00:00

I am developing an iPad app that has a large number of UIViewControllers ,

  • 0

I am developing an iPad app that has a large number of UIViewControllers, UITableViews (with cells with accessoryViews of UITextFields) etc, etc. Many of the UIViewControllers appear within a navigation hierarchy.

There are many different places where UITextFields appear, including as UITableViewCell accessoryViews.

I would like to devise an efficient strategy for dismissing the keyboard whenever the user touches outside the UITextField currently being edited. I have searched for keyboard dismiss techniques but have not yet found an answer that explains how a general keyboard dismiss strategy might work.

For example, I like this approach, where the following code is added to any ViewController:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"* * * * * * * * *ViewControllerBase touchesBegan");

    [self.view endEditing:YES]; // dismiss the keyboard

    [super touchesBegan:touches withEvent:event];
}

…but this technique does not deal with situations where, for example, a touch occurs within a UITableView which is on display. So, I’d need to add some code to call endEditing when a UITableView is touched etc, etc. Which means that my app will be liberally sprinkled with lots of code to dismiss the keyboard when various other UIElements are touched.

I guess I could just try and identify all the different places where touches need to be intercepted and the keyboard dismissed, but it seems to me that there may be a better design pattern somewhere for handling iOS keyboard dismiss events.

Can anyone share their experiences in this matter, and recommend a specific technique for generically handling keyboard dismissal across an entire app?

Many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:00:25+00:00Added an answer on June 7, 2026 at 1:00 am

    Your view hierarchy lives inside a UIWindow. The UIWindow is responsible for forwarding touch events to the correct view in its sendEvent: method. Let’s make a subclass of UIWindow to override sendEvent:.

    @interface MyWindow : UIWindow
    @end
    

    The window will need a reference to the current first responder, if there is one. You might decide to also use UITextView, so we’ll observe notifications from both text fields and text views.

    @implementation MyWindow {
        UIView *currentFirstResponder_;
    }
    
    - (void)startObservingFirstResponder {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(observeBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
        [center addObserver:self selector:@selector(observeEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
        [center addObserver:self selector:@selector(observeBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
        [center addObserver:self selector:@selector(observeEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
    }
    
    - (void)stopObservingFirstResponder {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:nil];
        [center removeObserver:self name:UITextFieldTextDidEndEditingNotification object:nil];
        [center removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil];
        [center removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
    }
    
    - (void)observeBeginEditing:(NSNotification *)note {
        currentFirstResponder_ = note.object;
    }
    
    - (void)observeEndEditing:(NSNotification *)note {
        if (currentFirstResponder_ == note.object) {
            currentFirstResponder_ = nil;
        }
    }
    

    The window will start observing the notifications when it’s initialized, and stop when it’s deallocated:

    - (id)initWithCoder:(NSCoder *)aDecoder {
        if ((self = [super initWithCoder:aDecoder])) {
            [self commonInit];
        }
        return self;
    }
    
    - (id)initWithFrame:(CGRect)frame {
        if ((self = [super initWithFrame:frame])) {
            [self commonInit];
        }
        return self;
    }
    
    - (void)commonInit {
        [self startObservingFirstResponder];
    }
    
    - (void)dealloc {
        [self stopObservingFirstResponder];
    }
    

    We’ll override sendEvent: to “adjust” the first responder based on the event, and then call super’s sendEvent: to send the event normally.

    - (void)sendEvent:(UIEvent *)event {
        [self adjustFirstResponderForEvent:event];
        [super sendEvent:event];
    }
    

    We don’t need to do anything about the first responder if there is no first responder. If there is a first responder, and it contains a touch, we don’t want to force it to resign. (Remember, there can be multiple touches simultaneously!) If there is a first responder, and a new touch appears in another view that can become the first responder, the system will handle that correctly automatically, so we also want to ignore that case. But if there is a first responder, and it doesn’t contain any touches, and a new touch appears in a view that can’t become first responder, we want to make the first responder resign.

    - (void)adjustFirstResponderForEvent:(UIEvent *)event {
        if (currentFirstResponder_
            && ![self eventContainsTouchInFirstResponder:event]
            && [self eventContainsNewTouchInNonresponder:event]) {
            [currentFirstResponder_ resignFirstResponder];
        }
    }
    

    Reporting whether an event contains a touch in the first responder is easy:

    - (BOOL)eventContainsTouchInFirstResponder:(UIEvent *)event {
        for (UITouch *touch in [event touchesForWindow:self]) {
            if (touch.view == currentFirstResponder_)
                return YES;
        }
        return NO;
    }
    

    Reporting whether an event contains a new touch in a view that can’t become first responder is almost as easy:

    - (BOOL)eventContainsNewTouchInNonresponder:(UIEvent *)event {
        for (UITouch *touch in [event touchesForWindow:self]) {
            if (touch.phase == UITouchPhaseBegan && ![touch.view canBecomeFirstResponder])
                return YES;
        }
        return NO;
    }
    
    @end
    

    Once you’ve implemented this class, you need to change your app to use it instead of UIWindow.

    If you’re creating your UIWindow in application:didFinishLaunchingWithOptions:, you need to #import "MyWindow.h" at the top of your AppDelegate.m, and then change application:didFinishLaunchingWithOptions: to create a MyWindow instead of a UIWindow.

    If you’re creating your UIWindow in a nib, you need to set the custom class of the window to MyWindow in the nib.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an iPad app that allows a user to select widgets to appear
this is my first ever question. I am developing a iPad app that allows
I am developing an app for iphone/ipad that I have some problems with. I
I'm developing a iPad app which has a popover which appears to show you
I am developing a iphone/ipad app. Problem is that when i change the orientation
I'm developing an iPad app that will be distributed in-house using the Enterprise Program
I'm developing an iPhone/iPad app that supports dragging items between table views. Since all
I'm developing an iPad app that needs read-only access to an Oracle database. Is
I am developing an iPad app that is designed to be released to private
Currently, I am developing an app that needs to store large amount of text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.