Easy way to dismiss keyboard?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Easy way to dismiss keyboard?

📝 Blog Post: Easy Way to Dismiss Keyboard: Say Goodbye to Looping Through Controls! 🖐️

Are you tired of spending endless hours looping through controls just to dismiss the keyboard in your app? 😩 We feel you! 😓

In this blog post, we're going to reveal a super easy solution to your problem, saving you time and hassle. 💪✨ Say goodbye to the tedious process of resigning all your controls as the first responder. 👋

The Problem: Getting the Current First Responder

So, you have a bunch of controls scattered throughout your table cells, and you're wondering if there's a simpler way to dismiss the keyboard in your app. 🤔 Specifically, you're curious about how to get the current first responder to the keyboard. Let's dive into it!

func getCurrentFirstResponder() -> UIResponder? {
    var responder: UIResponder? = nil
    
    // Loop through the windows
    for window in UIApplication.shared.windows {
        responder = window.perform(#selector(findFirstResponder))
        if responder != nil {
            break
        }
    }
    
    return responder
}

As you can see, the above code snippet allows you to retrieve the current first responder in your app by looping through the windows. 🌐✨ But fear not, we have a simpler solution for you!

The Easy Solution: UIResponder Extension 🎉

Instead of writing complex code and looping through all the controls, we can simplify the process by extending UIResponder. 🙌

extension UIResponder {
    static var currentFirstResponder: UIResponder? {
        _currentFirstResponder = nil
        UIApplication.shared.sendAction(#selector(findFirstResponder), to: nil, from: nil, for: nil)
        return _currentFirstResponder
    }
    
    private static weak var _currentFirstResponder: UIResponder?
    
    @objc private func findFirstResponder() {
        UIResponder._currentFirstResponder = self
    }
}

Now, you can easily dismiss the keyboard without going through all the hustle and bustle. 😌 Simply call UIResponder.currentFirstResponder?.resignFirstResponder() and voilà! Your keyboard is gone! 💨

Engage With Us!

We hope this easy solution saves you lots of time and frustration. 🕒⏳ Don't hesitate to share your thoughts and experiences in the comments section below. We would love to hear from you! 😊

If this blog post helped you, please give it a thumbs-up and share it with your fellow developers! 👍🔀 Together, we can make the coding journey smoother for everyone!

Happy coding! 💻✨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my