ios - Calling function from another ViewController in swift

Ios - Calling function from another ViewController in swift

Calling a function from another view controller in Swift depends on the relationship between the two view controllers. Here are a couple of common scenarios:

  1. Using Delegation: If you have a parent-child relationship between the view controllers or you want to notify one view controller from another, delegation is a common pattern.

  2. Using Segues: If you're transitioning from one view controller to another using segues, you can pass data or call methods during the segue process.

Here's an example of each:

Using Delegation:

In this scenario, View Controller A delegates a task to View Controller B, and View Controller B executes the task and informs View Controller A when it's done.

// Protocol defined in ViewControllerB.swift protocol ViewControllerBDelegate: AnyObject { func didPerformTask() } // ViewControllerB.swift class ViewControllerB: UIViewController { weak var delegate: ViewControllerBDelegate? func performTask() { // Perform task // Inform the delegate when done delegate?.didPerformTask() } } // ViewControllerA.swift class ViewControllerA: UIViewController, ViewControllerBDelegate { func didPerformTask() { // Task completed } func goToViewControllerB() { let viewControllerB = ViewControllerB() viewControllerB.delegate = self navigationController?.pushViewController(viewControllerB, animated: true) } } 

Using Segues:

If you're transitioning from View Controller A to View Controller B using segues, you can prepare for the segue in View Controller A and pass data or call methods in View Controller B during the segue process.

// ViewControllerA.swift class ViewControllerA: UIViewController { override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "YourSegueIdentifier" { if let viewControllerB = segue.destination as? ViewControllerB { viewControllerB.performTask() } } } } // ViewControllerB.swift class ViewControllerB: UIViewController { func performTask() { // Perform task } } 

Ensure you set the segue identifier in Interface Builder and the correct class for the destination view controller if you're using segues. If you're using a different navigation method, adjust accordingly.

Examples

  1. "iOS Swift call function from another ViewController"

    Description: This query revolves around calling a function defined in another ViewController in Swift.

    // Call function from another ViewController if let viewController = storyboard?.instantiateViewController(withIdentifier: "AnotherViewControllerIdentifier") as? AnotherViewController { viewController.someFunction() } 
  2. "iOS Swift communicate between ViewControllers"

    Description: This query focuses on communication between ViewControllers, including calling functions.

    // Communicate between ViewControllers to call a function if let anotherVC = self.tabBarController?.viewControllers?[1] as? AnotherViewController { anotherVC.someFunction() } 
  3. "iOS Swift pass data between ViewControllers"

    Description: This query involves passing data and calling functions between ViewControllers.

    // Pass data and call function between ViewControllers if let destinationVC = storyboard?.instantiateViewController(withIdentifier: "DestinationViewController") as? DestinationViewController { destinationVC.receivedData = someData destinationVC.someFunction() navigationController?.pushViewController(destinationVC, animated: true) } 
  4. "iOS Swift invoke method from another ViewController"

    Description: This query relates to invoking methods defined in another ViewController in Swift.

    // Invoke method from another ViewController if let anotherVC = navigationController?.viewControllers.first(where: { $0 is AnotherViewController }) as? AnotherViewController { anotherVC.someMethod() } 
  5. "iOS Swift call function in different ViewController"

    Description: This query focuses on calling a function located in a different ViewController in Swift.

    // Call function in a different ViewController if let otherVC = self.storyboard?.instantiateViewController(withIdentifier: "OtherViewController") as? OtherViewController { otherVC.someFunction() } 
  6. "iOS Swift access method from another ViewController"

    Description: This query involves accessing methods from another ViewController in Swift.

    // Access method from another ViewController if let targetVC = navigationController?.viewControllers.first(where: { $0 is TargetViewController }) as? TargetViewController { targetVC.someMethod() } 
  7. "iOS Swift call function from different ViewController"

    Description: This query relates to calling a function defined in a different ViewController in Swift.

    // Call function from a different ViewController if let otherViewController = self.tabBarController?.viewControllers?[1] as? OtherViewController { otherViewController.someFunction() } 
  8. "iOS Swift execute method from another ViewController"

    Description: This query involves executing methods from another ViewController in Swift.

    // Execute method from another ViewController if let targetVC = navigationController?.viewControllers.first(where: { $0 is TargetViewController }) as? TargetViewController { targetVC.executeMethod() } 
  9. "iOS Swift send data and call method between ViewControllers"

    Description: This query focuses on sending data and calling methods between ViewControllers in Swift.

    // Send data and call method between ViewControllers if let destinationVC = storyboard?.instantiateViewController(withIdentifier: "DestinationViewController") as? DestinationViewController { destinationVC.receivedData = someData destinationVC.someMethod() navigationController?.pushViewController(destinationVC, animated: true) } 
  10. "iOS Swift access function from another ViewController"

    Description: This query involves accessing a function defined in another ViewController in Swift.

    // Access function from another ViewController if let targetVC = navigationController?.viewControllers.first(where: { $0 is TargetViewController }) as? TargetViewController { targetVC.accessFunction() } 

More Tags

xhtmlrenderer dnf python-dataclasses output-formatting xelement securestring mongodb-query mybatis automationanywhere shorthand

More Programming Questions

More Stoichiometry Calculators

More Animal pregnancy Calculators

More Pregnancy Calculators

More Bio laboratory Calculators