iOS - Calling App Delegate method from ViewController

Cover Image for iOS - Calling App Delegate method from ViewController
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

šŸ“±šŸ“ž Calling App Delegate method from ViewController in iOS - Easy Solutions! šŸ§©

šŸ¤” Have you ever wondered how to call an App Delegate method from a ViewController in iOS? Maybe you ran into a problem where you needed to trigger a function in a different view controller from a button click, without using Interface Builder (IB)? Well, fret not! In this blog post, we'll explore some easy solutions to this common issue and help you navigate the sometimes tricky world of iOS development. Let's dive in! šŸŠā€ā™‚ļøšŸ’„

šŸš€ The Problem: You want to create a button in code that, when tapped, takes you to a different view controller and triggers a function in that new view controller. You are unable to use IB for this task.

šŸŽÆ The Solution: To accomplish this, you can take advantage of the shared instance of the App Delegate and make use of the UIApplication.shared.delegate property. Follow these simple steps:

1ļøāƒ£ Step 1: Access the App Delegate instance In your ViewController, import the UIKit framework and declare a property to hold the App Delegate instance:

import UIKit

class MyViewController: UIViewController {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    // ...
}

2ļøāƒ£ Step 2: Call the App Delegate method Now, you can call any method from the App Delegate by accessing it through the appDelegate property:

appDelegate.myMethod(argument: "Hello!")

3ļøāƒ£ Step 3: Implement the required method in the App Delegate In your App Delegate, define the myMethod method to perform the desired functionality:

func myMethod(argument: String) {
    // Do something with the argument
}

šŸ” Example Scenario: Let's assume you have two view controllers: SplashViewController and WalkthroughViewController. In SplashViewController, you have buttons for each room, and when a button is tapped, it should navigate to WalkthroughViewController and trigger a function to display the corresponding room walkthrough.

In SplashViewController:

@IBAction func roomButtonTapped(_ sender: UIButton) {
    guard let walkthroughVC = storyboard?.instantiateViewController(withIdentifier: "WalkthroughViewController") as? WalkthroughViewController else {
        return
    }
    appDelegate.navigateToWalkthrough(selectedRoom: sender.tag, walkthroughVC)
}

In the App Delegate:

func navigateToWalkthrough(selectedRoom: Int, _ walkthroughVC: WalkthroughViewController) {
    walkthroughVC.displayRoom(selectedRoom)
    window?.rootViewController = walkthroughVC
    window?.makeKeyAndVisible()
}

šŸ”” Call-to-Action: Now that you've learned how to call the App Delegate method from a ViewController effortlessly, it's time to put this knowledge into action! Start incorporating this technique into your own projects and let us know how it goes. šŸš€šŸ˜Ž

Share your experience in the comments below! Have you encountered any other challenges related to iOS development that you'd like us to cover in future blog posts? Let us know! And don't forget to share this post with your fellow developers who might find it helpful. Happy coding! šŸ’»šŸ™Œ

Disclaimer: The use of "IB" instead of Interface Builder is purely for brevity and familiarity. Please note that Storyboards and Interface Builder are still part of modern iOS development and offer powerful features.

šŸ“š References:

šŸ”— Connect with us on social media: šŸ¦ Twitter šŸ“˜ Facebook šŸ“· Instagram šŸŒ Website


More Stories

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

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ 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

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# 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

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# 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

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# 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

Matheus Mello
Matheus Mello