skip to Main Content
protocol useForDist {
   func findpindist()
}

class ViewController1: UIViewController, MKMapViewDelegate, useForDist {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func findpindist() {
        print("Test")
    }
}

class HomePageController: UIViewController {
    var delegate: useForDist?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate?.findpindist()
    }
}

Why is this not printing Test? Am I missing an aspect? I do not want a segue to a different view controller or a pop-up of one or a displaying. I’m new to XCode/Swift so if this is easily solvable bear with me 🙂

2

Answers


  1. Alright I have a couple of suggestions for you!! First off, I would change your function to have public on the front of it. So it would look something like this:

        public func findpindist() {
            print("Test")
        }
    

    Put is outside of your viewcontroller class and you should be able to call it in homePageViewController by putting findpindist() in the viewDidLoad.

    However, this looks sloppy and is very buggy so I have a better solution for you! There is something called NotificationCenter. This will allow you to pass data between viewcontrollers without a segue or anything. To use this, first put this code inside the viewDidLoad of the homePageController

       NotificationCenter.default.post(name: Notification.Name("findpindist"), object: nil, userInfo: ["message":"Test"])
    

    Next, add this code to your viewController1. the observer stays in the viewDidLoad and the objective c function goes outside of the viewDidLoad

    override func viewDidLoad() {
            super.viewDidLoad()
            NotificationCenter.default.addObserver(self, selector: #selector(findpindist(_:)), name: Notification.Name("findpindist"), object: nil)
        }
        
        @objc func findpindist(_ notification: Notification){
            let message = notification.userInfo!["message"] as! String
            print(message)
        }
    

    As long as you have your viewController1 loaded, it should print the statement. you can use this code to send any data between viewControllers. This should help with what I think you are trying to achieve, but as the other person said, I’m not sure what your objective is.

    heres some additional resources:
    Posting Messages with NotificationCenter
    A deep walkthrough of NotificationCenter

    Login or Signup to reply.
  2. You forgot a few things.

    first, you need to access to second ViewController ViewController from the first ViewController HomeViewController.

    Second, you need to set the HomeViewController delegate.

    import UIKit
    
    protocol useForDist: AnyObject {
       func findpindist()
    }
    
    class HomePageController: UIViewController {
        
        weak var delegate: useForDist?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // First
            let VC = ViewController()
            
            // Second
            delegate = VC
            
            delegate?.findpindist()
        }
    }
    
    
    class ViewController: UIViewController, useForDist {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        func findpindist() {
            print("Test")
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search