2

Here is the first VC where I am setting delegate:

class DiscoverVC: UIViewController, SetLocationDelegate {
    var name = ""
    let loc = LocationVC()
     override func viewDidLoad() {
        super.viewDidLoad()
        self.loc.delegate = self
     }

      func getLocation(loc: String) {
        self.name = loc
        self.tableView.reloadData()
    }
 }

And here is the second view Controller from where I'm getting the data via delegate:

protocol SetLocationDelegate: class {
    func getLocation(loc: String)
}

class LocationVC: UIViewController {

    weak var delegate: SetLocationDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate?.getLocation(loc: "Sam")
    }
}

Whenever I try to pass data it doesn't call the method and delegate doesn't get call. Help much needed.

Wismin
  • 1,135
  • 7
  • 21
pravin nagargoje
  • 70
  • 1
  • 1
  • 9
  • Please check In LocationVC controller's viewDidLoad() method called or not ? – vikas prajapati May 24 '17 at 13:46
  • Using `strong` reference of the delegate will work for you currently `weak var delegate: SetLocationDelegate?` is weak reference to delegate change it to `var delegate: SetLocationDelegate?` and it will work. – Nishant Bhindi May 24 '17 at 13:57
  • 2
    @NishantBhindi that would create a retain cycle which causes a memory leak. Do not do this. – shallowThought May 24 '17 at 14:50
  • 1
    Either `viewDidLoad` of `LocationVC` is never called due to the instantiation with `LocationVC()` (you can easily check by placing a breakpoint in viewDidLoad) or `viewDidLoad` of `LocationVC` is called before `viewDidLoad` of `DiscoverVC`, i.e. before the delegate is set. – shallowThought May 24 '17 at 14:52

1 Answers1

6

Note: My previous answer was using Storyboard. But since the questioner didn't want to use storyboard, I replace my answer without using storyboard. [ this answer was inspired by https://stackoverflow.com/a/41095757/3549695 ]

First, delete the Main.storyboard. Then in Project -> Deployment Info -> Main Interface (pick LaunchScreen instead of 'Main')enter image description here

Then on AppDelegate.swift modify didFinishLaunching with the following:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    let discoverVC = DiscoverVC() as UIViewController
    let navigationController = UINavigationController(rootViewController: discoverVC)
    navigationController.navigationBar.isTranslucent = false
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

    return true
}

The DiscoverVC.swift looks like this:

import UIKit

class DiscoverVC: UIViewController, SetLocationDelegate {

    var name = ""

    // to instantiate LocationVC once only for testing
    var notVisted = true

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .yellow 
        loadLocationVCOnlyOnce()
    }

    func loadLocationVCOnlyOnce() {
        // only visit one
        guard notVisted else { return }

        let locationVC = LocationVC()
        locationVC.delegate = self 
        self.navigationController?.pushViewController(locationVC, animated: true)

    }

    func getLocation(loc: String) {
        self.name = loc
        print(name)
    }
}

And the LocationVC looks like this:

import UIKit

protocol SetLocationDelegate: class {
    func getLocation(loc: String)
}

class LocationVC: UIViewController {

    weak var delegate: SetLocationDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .cyan 
        self.delegate?.getLocation(loc: "Sam")


    }

}

When you start it will automatically move from DiscoverVC (yellow background) to LocationVC (cyan background).

Then after you click the 'Back' button on top, you will see the 'Sam' printed in your console. And your view returned to DiscoverVC (yellow background).

Wismin
  • 1,135
  • 7
  • 21
  • I don't use storyboard. I prefer doing everything programmatically. For that how it can be done? – pravin nagargoje May 25 '17 at 05:06
  • I updated my answer above without using storyboard as per your request. And if the answer is satisfactory please accept and also upvote it. If there's any other issue, please comment. – Wismin May 25 '17 at 16:48